Monitor Your First Agent
In this tutorial you build a small currency agent in Python and trace it end to end. The finished trace shows a root agent observation with a nested tool call and an LLM generation that carries the model, token usage and cost.
Prerequisites
- An ANTS Platform project and permission to create API keys in it
- Python 3.9 or later
- An OpenAI API key (the agent makes one chat completion call)
Create API keys
In your project, open Settings > API Keys and create a key pair. Copy the secret key when it is shown. The dialog also shows the host URL of your ANTS Platform app. See API Keys.
Install the SDK
Set environment variables
Check the connection
AntsPlatform() reads the three ANTS_PLATFORM_* variables. auth_check() returns True when the keys and host are valid and raises an error when they are not. It is a blocking network call, so use it while setting up, not on every request.
Write the agent
Save this as agent.py:
What each part does:
start_as_current_observation(as_type="agent", agent_name=...)opens the root of the trace. The SDK requires anagent_nameon the outermost observation; everything opened inside it inherits that name. The SDK stores agent names in lowercase.convertopens a nested tool observation with its own input and output.from ants_platform.openai import OpenAIis a drop-in replacement for the OpenAI client. Each call is recorded as a generation with the model, messages, completion and token usage. The extraname=argument names the generation.update_tracesets the user, session and tags on the trace, so it appears under Users and Sessions.flush()sends buffered data before a short script exits. Long-running services flush automatically.
Run it
Find the trace
- Open Discover AI > Tracing and select the
currency-agenttrace. The observation tree showscurrency-agent, thenconvert-currencyandwrite-answerbeneath it. - Select
write-answerto see the prompt, completion, model, token counts and cost. - Open Discover AI > Sessions and select
demo-sessionto see every trace from that conversation. - After a few runs, open Gauge AI > Cost Dashboard to see spend by model.
Cost is calculated from the generation's model name and token usage, using the model prices under Settings > Models. If a model has no price, add one. See Models & Pricing.
Trace an LLM call without the OpenAI client
If you call a different provider, record the generation yourself and report usage:
Call it inside the agent observation, the same way convert is called.
Use the decorator for helper functions
@observe() turns a function into a nested observation and captures its arguments and return value. Decorated functions must run inside an observation that has an agent_name.
Record errors
An exception raised inside an observation is recorded on it. To record a handled failure, set the level and a status message:
Levels are DEBUG, DEFAULT, WARNING and ERROR. In Tracing, filter by level to find failing traces.
Add a score
Scores attach a quality signal, such as user feedback, to a trace:
See Scores.
Common issues
| Symptom | Fix |
|---|---|
ValueError: agent_name is required | Wrap the work in start_as_current_observation(..., as_type="agent", agent_name="..."). This also applies to @observe() functions and OpenAI drop-in calls made outside any observation. |
auth_check() fails | Check that both keys belong to the same project and that ANTS_PLATFORM_HOST is your app URL. |
| No trace appears after the script ends | Call ants.flush() before the process exits. |
| Generation has tokens but no cost | Add a price for the model under Settings > Models. |
More help: Debugging AI Agents.