You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs-website/docs/pipeline-components/agents-1/agent.mdx
+28-1Lines changed: 28 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -41,11 +41,12 @@ The `Agent` returns a dictionary containing:
41
41
-`step_count`: the number of steps the agent ran,
42
42
-`token_usage`: aggregated token usage summed across every LLM call in the run,
43
43
-`tool_call_counts`: how many times each tool was invoked, keyed by tool name,
44
+
-`exit_reason`: why the agent stopped, useful for routing its output downstream,
44
45
- Additional dynamic keys based on `state_schema`.
45
46
46
47
### Run Metadata
47
48
48
-
The `step_count`, `token_usage`, and `tool_call_counts` outputs are populated automatically during a run. They are added to the agent's `state_schema` behind the scenes, so tools registered with `inputs_from_state` can read them mid-run. They are outputs only — they cannot be passed as inputs to `run()` or `run_async()`, and using them as keys in your own `state_schema` raises a `ValueError`. See [State](./state.mdx#schema-definition) for details.
49
+
The `step_count`, `token_usage`, `tool_call_counts`, and `exit_reason` outputs are populated automatically during a run. They are added to the agent's `state_schema` behind the scenes, so tools registered with `inputs_from_state`and [hooks](./hooks.mdx)can read them from the live `State`. They are outputs only — they cannot be passed as inputs to `run()` or `run_async()`, and using them as keys in your own `state_schema` raises a `ValueError`. See [State](./state.mdx#schema-definition) for details.
49
50
50
51
```python
51
52
response = agent.run(messages=[ChatMessage.from_user("What is 7 * (4 + 2)?")])
The `exit_reason` output tells you why the agent stopped, which makes it easy to route the agent's output downstream — for example, with a [`ConditionalRouter`](../routers/conditionalrouter.mdx). It is one of:
65
+
66
+
-`"text"`: the model returned a reply with no tool calls.
67
+
- the name of the tool that satisfied a tool exit condition. In this case `last_message` is that tool's result — a tool-result `ChatMessage` whose `text` is empty — so `exit_reason` tells you how to consume it.
68
+
-`"max_agent_steps"`: the agent reached `max_agent_steps` before meeting an exit condition.
69
+
70
+
Because `exit_reason` is available on the live `State`, an `after_run`[hook](./hooks.mdx) can read it to react to how the run ended — for example, appending a fallback answer when the step budget is exhausted before the agent finished:
71
+
72
+
```python
73
+
from haystack.components.agents.state import State
74
+
from haystack.dataclasses import ChatMessage
75
+
from haystack.hooks import hook
76
+
77
+
78
+
@hook
79
+
deffallback_on_max_steps(state: State) -> None:
80
+
if state.get("exit_reason") =="max_agent_steps":
81
+
state.set(
82
+
"messages",
83
+
[ChatMessage.from_assistant("Sorry, I ran out of steps before finishing.")],
Copy file name to clipboardExpand all lines: docs-website/docs/pipeline-components/agents-1/state.mdx
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -70,7 +70,7 @@ If you don't specify a handler, State automatically assigns a default based on t
70
70
:::info Reserved keys
71
71
The `Agent` manages some state keys itself and rejects them in a user-provided `state_schema` with a `ValueError`:
72
72
73
-
- The run-metadata keys `step_count`, `token_usage`, and `tool_call_counts`, which the Agent populates automatically during a run: tools can read them mid-run via `inputs_from_state`, and they are returned in the result dictionary.
73
+
- The run-metadata keys `step_count`, `token_usage`, `tool_call_counts`, and `exit_reason`, which the Agent populates automatically during a run: tools and hooks can read them from the live `State`, and they are returned in the result dictionary. `exit_reason` reports why the Agent stopped (`"text"`, the name of the tool that triggered a tool exit condition, or `"max_agent_steps"`).
74
74
- The hook-facing keys `continue_run` (set by an `on_exit` hook to keep the Agent running), `tools` (the tools available in the current step, for hooks to inspect), and `hook_context` (the request-scoped resources passed to `Agent.run(hook_context={...})`).
75
75
76
76
If one of your state keys clashes, rename it (for example, `my_token_usage`).
0 commit comments