- Studio
- Observability
- Offline evaluations
Evaluate Agents SDK agents
Evaluate Agents SDK agents
If the system you evaluate is an agent built with the Agents SDK, pass it as an AgentSystem instead of describing a System by hand. The task runs the agent, and the run records which agent was evaluated, subagents included, so Studio can show it.
Before you begin
You need the Evaluation SDK (see Installation) and a Mistral API key. Install the Agents SDK, mistralai-agents 1.1.0 or later:
pip install -U mistralai-agents
The Agents SDK is not a dependency of mistralai-evaluations. The Evaluation SDK uses an agent from code through its own methods, and imports the Agents SDK only to reach a deployed agent.
Evaluate an agent
Create the agent, then write a task that runs it as ctx.agent:
import asyncio
import os
from mistralai import agents
from mistralai.evaluations import (
AgentSystem, Evaluation, Evaluator, Mistral, Project, ScorerContext, TaskContext,
)
client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])
@agents.tool
async def lookup_order(order_id: str) -> dict[str, str]:
"""Get the status of an order."""
return {"order_id": order_id, "status": "shipped on Monday"} # read from your database
agent = agents.Agent(
name="support-bot",
model="mistral-small-latest",
instructions="You are the support agent of Shopi. Look the order up before answering.",
tools=[lookup_order],
)
dataset = [
{"question": "Has order 1042 shipped?", "expected": "shipped"},
{"question": "What is the status of order 2077?", "expected": "shipped"},
]
async def task(ctx: TaskContext) -> str:
result = await ctx.agent.run(ctx.input_record["question"])
return result.text or ""
def scorer(ctx: ScorerContext) -> int:
return 1 if ctx.input_record["expected"] in str(ctx.output).lower() else 0
evaluators = [
Evaluator(
name="accuracy",
description="1 if the answer contains the expected status, 0 otherwise.",
scorer=scorer,
),
]
async def main():
run = await client.evaluation.run(
system=AgentSystem(agent=agent),
task=task,
dataset=dataset,
evaluators=evaluators,
project=Project(name="Support"),
evaluation=Evaluation(name="Support bot"),
)
run.show(level="run")
asyncio.run(main())
ctx.agent is the agent to run. It runs in your process, with its real tools and hooks.
ctx.agent is not the Agent itself. ctx.agent.run() is described in Turns that don't complete. Anything else (name, start_session(), the settings) is the Agent's. ctx.agent.agent is the Agent with the run's overrides applied.
Turns that don't complete
ctx.agent.run() takes what Agent.run() takes and returns the Completed turn.
Any other outcome raises AgentTurnError, with the Agents SDK's turn record as turn. This covers a turn that is Failed, Interrupted, or Blocked on an approval, a turn that is Busy because another turn of the session is active, a turn that is Running because its timeout expired, and an outcome that the Agents SDK doesn't know.
When a turn raises, the record fails and the evaluators don't score it. retry_failed_records() runs the record again.
A failed turn is missing from the statistics. Check run.failed_record_count before you read the scores. run.show() prints it.
A turn blocked on an approval fails its record too. To let the agent go on, answer the approvals in the task:
async def task(ctx: TaskContext) -> str:
result = await ctx.agent.run(
ctx.input_record["question"],
on_approval=lambda callback, request: agents.AllowApproval(),
)
return result.text or ""
With output_type=, a turn whose output doesn't validate is still Completed. Its output is an agents.InvalidOutput, which the task can check, or return for the evaluators to score.
Evaluate a variant
overrides changes some settings of the agent for this run only. Your Agent is not modified, and nothing is created or changed on a deployed agent.
system = AgentSystem(
agent=agent,
overrides={"instructions": "Always look the order up before answering.", "temperature": 0.0},
)
You can override any setting of the Agent that takes a plain value, except its name: for example model, instructions, temperature, max_iterations, reasoning_effort, or permission_mode. The Agent checks the values before the run starts, so a setting that a newer Agents SDK adds can be overridden as soon as you install it.
Tools, hooks, skills, and subagents are fixed once the Agent is built. To vary them, pass the function that builds the agent (see Optimize tools and skills). An agent built with its own completion= config sets its model, temperature, and reasoning effort there, so they can't be overridden.
Describe the run beyond the agent
params records what the run varies besides the agent's settings, such as the environment the agent runs in. The run records it with the overrides as the system's params, so you can compare runs on both:
system = AgentSystem(
agent=agent,
overrides={"temperature": 0.0},
params={"sandbox": "mistral"},
)
params is only recorded, never applied to the agent. A key goes in overrides or in params, not both. A setting of the Agent goes in overrides.
Optimize an agent
evaluation.optimize() takes an AgentSystem too. Wrap the settings to search in Tunable. Fixed values stay as they are.
from mistralai.evaluations import GEPA, Tunable
system = AgentSystem(
agent=agent,
overrides={
"instructions": Tunable(agent.instructions),
"temperature": Tunable(0.7, bounds=(0.0, 1.0)),
"max_iterations": 20,
},
)
result = await client.evaluation.optimize(
system=system, task=task, dataset=dataset, evaluators=evaluators, algo=GEPA(),
)
Each candidate is a run of the agent with the candidate's values. See Optimize prompts and parameters.
The task must take a ctx: TaskContext (annotated), because the agent is reached as ctx.agent. optimize() refuses an AgentSystem with any other task signature before the optimization starts.
result.winner.system (or result.best_attempt.system) holds the winning overrides. Evaluate the optimized agent again with them, then write the values into your Agent, or redeploy it:
if result.winner:
best = AgentSystem(agent=agent, overrides=result.winner.system)
For a deployed agent, the overrides are session settings. Put model and reasoning_effort into the agent's code before you redeploy it. additional_instructions can stay a session override.
Optimize tools and skills
Requires SDK 0.11.3+.
An Agents SDK tool or skill is fixed once built, so its description can't be overridden on the Agent. Write a function that builds the agent from the texts to vary, and pass that function as agent:
# support_bot.py
# The skill's name and description stay fixed: the Agents SDK refuses a skill without them.
SKILL_METADATA = "---\nname: refund-requests\ndescription: How to answer a refund request.\n---\n"
REFUND_REQUESTS_SKILL = "Look the order up first. Never promise a refund the store has not recorded.\n"
def lookup_order(args: Any) -> dict[str, str]: # the tool's handler, at module level
return store.lookup_order(args.order_id)
def build_agent(
*,
instructions: str = INSTRUCTIONS,
lookup_order_description: str = "Get the status of an order.",
order_id_description: str = "Order id, e.g. 112-1234567",
refund_requests_skill: str = REFUND_REQUESTS_SKILL,
) -> agents.Agent:
args = create_model("LookupOrderArgs", order_id=(str, Field(description=order_id_description)))
tool = agents.tool(lookup_order, name="lookup_order", description=lookup_order_description, input_schema=args)
skill = agents.LocalSkillBundle(
files=[agents.LocalSkillFile(path="SKILL.md", content_base64=base64.b64encode((SKILL_METADATA + refund_requests_skill).encode()).decode())]
)
return agents.Agent(
name="support-bot",
instructions=instructions,
tools=[tool],
skills=[skill],
environment=agents.BashkitSandbox(), # the agent reads its skills as files of its sandbox
)
agent = build_agent() # your production agent, unchanged
# evals/optimize.py
system = AgentSystem(
agent=build_agent,
overrides={
"lookup_order_description": Tunable("Get the status of an order."),
"order_id_description": Tunable("Order id, e.g. 112-1234567"),
"refund_requests_skill": Tunable(REFUND_REQUESTS_SKILL),
},
)
result = await client.evaluation.optimize(system=system, task=task, dataset=dataset, evaluators=evaluators, algo=GEPA())
- The overrides are the function's parameters. Parameters not in overrides keep their defaults, and evaluation.run() takes concrete values the same way (overrides={"lookup_order_description": "..."}).
- The function takes keyword parameters, or one parameter typed as a Pydantic model or a dataclass, which the SDK builds from the overrides (def build_agent(params: SupportBotParams)).
- A key that is not a parameter (or a field of that class) is refused when you declare the AgentSystem, with the valid names. A parameter without a default must be in overrides.
- The function is called when the run starts, once per candidate in optimize() (and once before the search, with the Tunable seeds, so a function that can't build the agent fails early). It is never called when you declare the AgentSystem.
- The system is named after the agent the function builds; AgentSystem(name=...) names it otherwise. The run records the built agent as it does an Agent, with the defaults the overrides replace.
- In a Mistral Workflow (the workflows evaluation plugin), agent must still be an Agent declared on the task: a function is not supported there yet.
result.winner.system holds the winning texts: copy them into the defaults of build_agent, or evaluate them again with AgentSystem(agent=build_agent, overrides=result.winner.system).
Tools that change data
Requires SDK 0.11.4+.
optimize() runs the agent once per candidate and record: a few hundred times. A tool that refunds an order, sends an email or creates a ticket does it for real each time. A tool may change data unless it says it only reads:
@agents.tool(annotations=agents.ToolAnnotations(read_only_hint=True))
def lookup_order(order_id: str) -> dict[str, str]:
"""Get the status of an order."""
return store.lookup_order(order_id)
@agents.tool # declares nothing: it may change data
def refund_order(order_id: str) -> dict[str, str]:
return store.refund_order(order_id)
By default, every tool runs, and optimize() tells you which ones may have changed data: each generation's metadata counts their calls per tool ({"may_write_tool_calls": {"refund_order": 2}}), and a warning at the end of the run lists them:
[warning] 24 tool calls of the agent ran, and their tools may change data (refund_order x24): each call did
it for real. Block them with AgentSystem(allow_side_effects=False), mock them with mocked_tools, name those
that only read in read_only_tools, or set allow_side_effects=True to silence this warning
AgentSystem(allow_side_effects=False) blocks them instead, in run() and optimize():
-
This covers every tool call of the agent and of its subagents: its Python tools, MCP servers and connectors (the server declares readOnlyHint). A tool that declares nothing may change data. The built-in tools of the agent's sandbox run.
-
A call that does not run reaches the model as a failed call ("Not run during this optimization: this tool may change data" for a Python tool; an MCP server's or a connector's tool reads that its approval was refused), and the record goes on. Each generation's metadata counts those calls per tool ({"blocked_tool_calls": {"refund_order": 2}}), and the run logs the total.
-
A tool that only reads but declares nothing, often one of a third-party MCP server, can be named in read_only_tools, by its name or ID (* matches any characters): it is neither blocked nor counted.
system = AgentSystem(agent=build_agent, read_only_tools=["search_issues", "drive.get_*"]) -
The task's own on_approval (ctx.agent.run(content, on_approval=...)) still answers the calls the guard lets through. An agent with a permission_policy keeps the calls it denies denied.
allow_side_effects on the AgentSystem sets what happens to the calls that may change data:
allow_side_effects | optimize() | run() |
|---|---|---|
| None (default) | run, counted, and listed in a warning | run |
| False | blocked | blocked |
| True | run | run |
Mock a tool
A blocked call tells the model the tool did not run, which is not what a real conversation looks like. mocked_tools answers a Python tool in its place: the model gets a plausible result, and the tool never runs its code, in run() too. A mock is a function called like the tool's own function, or from_record("<path>"), the value at that path of the record the task runs (keys joined by dots):
from mistralai.evaluations import from_record
def mock_refund(order_id: str) -> dict[str, str]:
return {"order_id": order_id, "status": "pending", "refund_id": "rf-test"}
dataset = [
{"message": "Refund my order 112-7730194", "fixtures": {"order": {"status": "delivered"}}},
]
system = AgentSystem(
agent=build_agent,
overrides={"lookup_order_description": Tunable("Get the status of an order.")},
mocked_tools={
"refund_order": mock_refund,
"lookup_order": from_record("fixtures.order"),
},
)
- A record without a value at the path does not run the tool: the call is blocked, as above.
- The keys are the names of the agent's Python tools (or its subagents'); another name is refused when the run starts. The tools of an MCP server or a connector can be blocked, not mocked.
- Mocking needs an agent from code. A deployed agent's tools run on the platform, which tells neither what they are nor whether they change data: they can be neither blocked, mocked nor counted, and optimize() warns how many turns it runs, unless AgentSystem(agent="support-bot", allow_side_effects=True).
Evaluate a deployed agent
Pass the name or id of a deployed agent to evaluate the deployment itself. Each ctx.agent.run() runs a session on it, so the task needs no code of the agent. The session is cancelled once its turn returns, so it does not count toward the agent's concurrent sessions afterwards; its history stays readable. A run that raises with an idempotency_key keeps its session, for a retry with that key. This needs mistralai-evaluations 0.10.2 or later.
system = AgentSystem(agent="support-bot", overrides={"model": "mistral-medium-latest"})
This needs the Agents SDK installed and a run saved in Studio (not local=True). The agent is looked up and run with the client's credentials, on the same server as the evaluation.
A deployed agent is changed per session. The overrides are what the Agents SDK lets a session change: model, reasoning_effort, and additional_instructions, which is appended to the deployed instructions. See SessionAgentCustomization.
ctx.agent.run() passes its options to the deployment like Agent.run() does. With output_type=, the Agents SDK validates the answer against the type, but the deployment keeps its own completion config. Unless its instructions ask for that shape, expect an agents.InvalidOutput.
The evaluation client looks the agent up with its HTTP settings (proxy, TLS). The sessions of the deployed agent go through the Agents SDK's own HTTP client, which only picks up proxies from the environment (HTTPS_PROXY). With mistralai 3, whose client runs on httpx2, the lookup uses the Agents SDK's own HTTP settings too.
Credentials
The Agents API is reached as the evaluation client. A client with an API key sends it.
A client without an API key, whose HTTP client sets the Authorization header on every request, sends every Agents API call through that HTTP client: the agent lookups and the sessions of a deployed agent. See Credentials without an API key. In a Mistral Workflow, these calls run as the worker's service account on a managed deployment. They run as the user who executes the workflow when it is declared with on_behalf_of=True, so a deployed agent is found in that user's workspace and runs as that user.
With mistralai 3, whose client runs on httpx2, the Agents SDK gets an httpx client with the same credentials, headers, timeout, and TLS settings (CA bundle, client certificate). Like the httpx2 client, it reads its proxy from the environment (HTTPS_PROXY) unless that client was built with trust_env=False. A proxy set on the httpx2 client itself is not carried over.
An agent from code runs in your process. Its model calls, connectors, and MCP servers use the credentials of its own environment (MISTRAL_API_KEY), whatever the evaluation client carries.
Run the agent elsewhere
The task doesn't have to call ctx.agent.run(...). When your agent runs in another process or a sandbox, read the settings of ctx.agent (the agent with the overrides applied) and pass them to where it runs. The run still records the agent, its variants, and its optimization:
from my_agent import agent # the same Agent your runner builds
async def task(ctx: TaskContext) -> str:
return await run_in_sandbox(
prompt=ctx.input_record["prompt"],
model=ctx.agent.model,
instructions=ctx.agent.instructions,
max_iterations=ctx.agent.max_iterations,
)
system = AgentSystem(
agent=agent,
overrides={"model": "mistral-medium-latest"},
params={"sandbox": "docker"},
)
Forward every setting your runner applies: the run records the config of ctx.agent, not what the runner did with it.
What the run records
The run's system has kind="agent". It holds:
- the overrides and params as its params: what varies between runs, compared and optimized;
- the agent as evaluated, as its spec, recorded as the Agents SDK describes it.
A System built by hand has kind="custom" and no spec.
For an agent from code, the spec is its config as the Agents SDK serializes it (model and completion settings, instructions, builtins, permissions, subagents), with the overrides applied. The Agents SDK adds the agent's tools, hooks, skills, MCP servers, and connectors only when a session starts and doesn't expose them otherwise, so the spec records them as unknown (null). A subagent is compiled with its own tools and hooks, so those are recorded.
For a deployed agent, the spec is the agent's deployed record, as deployed. The session overrides are the run's params.
Both also keep, in spec.base, the values the overrides replace: one entry per override, or null when it replaces nothing, as additional_instructions does. This tells the overrides apart from params.
With an Agents SDK version that can't serialize an agent, the run still starts. The config of an agent from code is recorded as unknown (null) and a warning is logged.
The run stores the config as is. Keep credentials out of it, as the Agents SDK does: it references them by environment variable or workspace secret, never by value.
Link to the deployed agent
When a deployed agent of the workspace has the same name (or the run evaluates a deployed agent), the run is linked to it, so Studio can open the agent from its runs. A link by name is marked as such, because the deployed agent may run other code than the one you evaluated.
If the Agents API can't be reached or rejects the client's credentials, the run does not start.
Retry failed records
To retry the failed records of an agent run, pass the same AgentSystem to retry_failed_records(). The retry is refused if the agent's config (subagents included) changed since the run, so the records of one run always come from one agent.
- Settings that the environment sets (the completion gateway URL and key variable) are not compared.
- Upgrading the Agents SDK between the run and the retry can change the serialized config and refuse the retry.
- For a deployed agent, the retry compares the agent id and its deployed settings (model, instructions, permissions), not when it was deployed. Redeploying the same agent doesn't refuse the retry. A redeploy that only changes the agent's code, tools, or skills is not detected, because the deployed record doesn't describe them.
Troubleshooting
Problem | Cause | Solution |
|---|---|---|
| The scores look better than the agent really is. | Failed turns are missing from the statistics. | Check run.failed_record_count, then call retry_failed_records(). |
| Records fail with AgentTurnError on a Blocked turn. | The agent asked for an approval and nothing answered it. | Pass on_approval= to ctx.agent.run(). |
| optimize() refuses the AgentSystem before it starts. | The task doesn't take a single ctx: TaskContext argument. | Annotate the task parameter as TaskContext and call ctx.agent.run(). |
| An override is refused before the run starts. | The Agent rejected the value, or the setting is not a plain value (tools, hooks, skills, subagents). | Fix the value, or pass a function that builds the agent for what is not a plain setting (see Optimize tools and skills). |
| retry_failed_records() refuses the retry. | The agent's config changed since the run, or the Agents SDK version changed. | Pass the same AgentSystem, with the same Agents SDK version as the run. |
Next steps
Configure system params
The System you use for pipelines that are not an Agents SDK agent.
Optimize prompts and parameters
Search for a better agent configuration.
Retry failed records
Rerun the records that failed.
Evaluate agents in a workflow
Run the same evaluation in Mistral Workflows.