Tracing and monitoring
Every agent run returns a trace of the steps it took, the tools it called, the credits it used and how long it ran.
Tracing is on by default. Read it from the run result, with no extra setup, or monitor every run in the aixplain app dashboard.
Try it
from aixplain import Aixplain
aix = Aixplain(api_key="YOUR_API_KEY")
web_search = aix.Tool.get("tavily/tavily-web-search/tavily")
web_search.allowed_actions = ["search"]
agent = aix.Agent(
name="Traced research agent",
description="Researches topics on the web.",
instructions="Use web search for anything time-sensitive. Answer in two sentences.",
tools=[web_search],
).save()
result = agent.run("What are the most popular programming languages this year?")
print("Status: ", result.status)
print("Steps: ", [step["unit"]["name"] for step in result.data.steps])
print("Credits:", result.used_credits)
print("Time: ", round(result.run_time, 1), "s")
Inspecting steps
result.data.steps is a list with one entry per model or tool call. Each step names the agent that ran it (agent) and the model or tool it called (unit).
for number, step in enumerate(result.data.steps, start=1):
print(f"{number}. {step['agent']['name']} -> {step['unit']['name']} ({step['unit']['type']})")
print(" input: ", str(step["input"])[:80])
print(" output:", str(step["output"])[:80])
print(" credits:", step["used_credits"], "| time:", step["run_time"], "| status:", step["status"])
| Field | Description |
|---|---|
agent | The agent that ran the step, as a dict with id and name. In a team, this is the subagent or inspector. |
unit | The model or tool called, as a dict with id, name and type (model or tool). |
input, output | What the unit received and returned. |
thought | The agent's reasoning before the step, when the LLM provides it. |
status | Step outcome. |
used_credits, api_calls | Cost and number of API calls for the step. |
input_tokens, output_tokens, total_tokens | Token counts for model steps. |
start_time, end_time, run_time | Step timing. |
action | For inspector steps, the action taken: continue, rerun, edit or abort. |
execution_id | On a delegate_task step, the ID of the subagent's own run. See Trace subagents. |
Execution metrics
result.data.execution_stats sums up the run.
stats = result.data.execution_stats
print("API calls:", stats["api_calls"])
print("Credits: ", stats["credits"])
print("Runtime: ", round(stats["runtime"], 1), "s")
print("Session: ", stats["session_id"])
print("Request: ", stats["request_id"])
Keep request_id when you contact support about a run.
Trace subagents
An agent with subagents returns one trace for the whole team. The agent field on each step tells you which agent did the work, and each handoff to a subagent is a delegate_task step.
researcher = aix.Agent(
name="Trace researcher",
description="Finds facts on the web.",
instructions="Search the web and return the key facts.",
tools=[web_search],
)
writer = aix.Agent(
name="Trace writer",
description="Writes short, clear summaries from research notes.",
instructions="Write a three-sentence summary of the notes you receive.",
)
team = aix.Agent(
name="Traced research team",
description="Researches a topic, then writes a summary.",
instructions="Always have the researcher gather facts first, then the writer summarize them.",
agents=[researcher, writer],
).save(save_subcomponents=True)
result = team.run("Give me a short summary of how the Eiffel Tower was built.")
for step in result.data.steps:
print(step["agent"]["name"], "->", step["unit"]["name"], step.get("execution_id", ""))
A subagent saved as its own agent, as save(save_subcomponents=True) does, runs as a separate execution. Its delegate_task step carries that run's execution_id instead of the subagent's steps. Fetch them with poll().
for step in result.data.steps:
if step.get("execution_id"):
subagent_run = team.poll(step["execution_id"])
print(step["execution_id"], [s["unit"]["name"] for s in subagent_run.data.steps])
Watch a run live
Pass progress_format to print each step while the run happens. Use "status" for one line, or "logs" for a timeline.
result = agent.run("What is the latest stable Python release?", progress_format="logs")
progress_verbosity=2 adds the agent's thoughts, and 3 adds full inputs and outputs.
Warnings and errors
A run that reaches a limit, such as its step limit or its budget, can still succeed. It then returns SUCCESS with a warning in result.warnings, each with a code and a message.
agent.budget.max_iterations = 1
agent.save()
result = agent.run("What are the most popular programming languages this year?")
print(result.status)
for warning in result.warnings:
print(warning["code"], "-", warning["message"])
A run that fails reports its code in error_details.code. diagnostic_error_codes is deprecated: read warnings[].code and error_details.code instead.
Monitor in the dashboard
Every run also appears in the aixplain app dashboard, with its steps, cost and status. Use it to watch usage and cost across your agents and to open the trace of any run.
Next steps
- Inspectors: check inputs, steps and outputs while a run happens.
- Deployment: call your agent from production code.
- API reference: agent: every parameter and method.