diff --git a/docs-website/docs/pipeline-components/agents-1/agent.mdx b/docs-website/docs/pipeline-components/agents-1/agent.mdx index 8c6e3c1aa61..8024bf2d12b 100644 --- a/docs-website/docs/pipeline-components/agents-1/agent.mdx +++ b/docs-website/docs/pipeline-components/agents-1/agent.mdx @@ -38,16 +38,33 @@ The `Agent` returns a dictionary containing: - `messages`: the full conversation history, - `last_message`: the final `ChatMessage` from the agent, +- `step_count`: the number of steps the agent ran, +- `token_usage`: aggregated token usage summed across every LLM call in the run, +- `tool_call_counts`: how many times each tool was invoked, keyed by tool name, - Additional dynamic keys based on `state_schema`. +### Run Metadata + +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. + +```python +response = agent.run(messages=[ChatMessage.from_user("What is 7 * (4 + 2)?")]) + +print(response["step_count"]) # 2 +print( + response["token_usage"], +) # {"prompt_tokens": 512, "completion_tokens": 86, ...} +print(response["tool_call_counts"]) # {"calculator": 1} +``` + ## Parameters `chat_generator` is the only mandatory parameter — an instance of a Chat Generator that supports tools. All other parameters are optional. -- `tools`: A list of tool or toolset instances the agent can call. Supported types: [`Tool`](../../tools/tool.mdx), [`ComponentTool`](../../tools/componenttool.mdx), [`PipelineTool`](../../tools/pipelinetool.mdx), [`MCPTool`](../../tools/mcptool.mdx), [`Toolset`](../../tools/toolset.mdx), [`MCPToolset`](../../tools/mcptoolset.mdx), [`SearchableToolset`](../../tools/searchabletoolset.mdx). +- `tools`: A list of tool or toolset instances the agent can call. Supported types: [`Tool`](../../tools/tool.mdx), [`ComponentTool`](../../tools/componenttool.mdx), [`PipelineTool`](../../tools/pipelinetool.mdx), [`MCPTool`](../../tools/mcptool.mdx), [`Toolset`](../../tools/toolset.mdx), [`MCPToolset`](../../tools/mcptoolset.mdx), [`SearchableToolset`](../../tools/searchabletoolset.mdx). Tool names must be unique; duplicate names are detected at the start of each agent step, before the chat generator is called. - `system_prompt`: A plain string or Jinja2 template used as the system message for every run. If the template contains Jinja2 variables, those variables become additional inputs to `run()`. - `user_prompt`: A Jinja2 template appended to the user-provided messages on each run. Template variables become additional inputs to `run()`. Use `required_variables` to enforce which variables must be provided. -- `exit_conditions`: List of conditions that cause the agent to stop. Use `”text”` to stop when the LLM replies without a tool call, or a tool name to stop once that tool has been executed. Defaults to `[“text”]`. +- `exit_conditions`: List of conditions that cause the agent to stop. Use `”text”` to stop when the LLM replies without a tool call, or a tool name to stop once that tool has been executed. Defaults to `[“text”]`. Exit conditions are evaluated at runtime rather than validated at initialization, so a condition can name a tool that is only loaded later — for example, a tool passed at runtime via `run(tools=...)` or one discovered by a [`SearchableToolset`](../../tools/searchabletoolset.mdx). - `state_schema`: Defines the agent's runtime state — a dict mapping key names to type configs (e.g. `{“docs”: {“type”: list[Document]}}`). Tools can read from and write to state keys via `inputs_from_state` and `outputs_to_state`. See [State](./state.mdx) for full details. - `streaming_callback`: A callback invoked for each streamed token. Use the built-in `print_streaming_chunk` for console output. - `max_agent_steps`: Maximum number of LLM + tool call iterations before the agent stops. Defaults to `100`. diff --git a/docs-website/docs/pipeline-components/agents-1/state.mdx b/docs-website/docs/pipeline-components/agents-1/state.mdx index 1a124636dcc..9c089f8d7a6 100644 --- a/docs-website/docs/pipeline-components/agents-1/state.mdx +++ b/docs-website/docs/pipeline-components/agents-1/state.mdx @@ -67,6 +67,15 @@ The schema defines what data can be stored and how values are updated. Each sche If you don't specify a handler, State automatically assigns a default based on the type. +:::info Reserved keys +The `Agent` manages some state keys itself and rejects them in a user-provided `state_schema` with a `ValueError`: + +- 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. +- 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={...})`). + +If one of your state keys clashes, rename it (for example, `my_token_usage`). +::: + ### Default Handlers State provides two built-in merge behaviors (importable from `haystack.components.agents.state`): diff --git a/docs-website/docs/pipeline-components/generators.mdx b/docs-website/docs/pipeline-components/generators.mdx index f18a6503a00..a5cfc6b3f44 100644 --- a/docs-website/docs/pipeline-components/generators.mdx +++ b/docs-website/docs/pipeline-components/generators.mdx @@ -35,6 +35,7 @@ Generators are responsible for generating text after you give them a prompt. The | [LlamaStackChatGenerator](generators/llamastackchatgenerator.mdx) | Enables chat completions using an LLM model made available via Llama Stack server | ✅ | | [MetaLlamaChatGenerator](generators/metallamachatgenerator.mdx) | Enables chat completion with any model hosted available with Meta Llama API. | ✅ | | [MistralChatGenerator](generators/mistralchatgenerator.mdx) | Enables chat completion using Mistral's text generation models. | ✅ | +| [MockChatGenerator](generators/mockchatgenerator.mdx) | Returns predefined responses without calling any API — a deterministic, zero-cost stand-in for real Chat Generators in tests and prototypes. | ✅ | | [NvidiaChatGenerator](generators/nvidiachatgenerator.mdx) | Enables chat completion using Nvidia-hosted models. | ✅ | | [NvidiaGenerator](generators/nvidiagenerator.mdx) | Provides an interface for generating text using LLMs self-hosted with NVIDIA NIM or models hosted on the NVIDIA API catalog. | ❌ | | [OllamaChatGenerator](generators/ollamachatgenerator.mdx) | Enables chat completion using an LLM running on Ollama. | ✅ | diff --git a/docs-website/docs/pipeline-components/generators/mockchatgenerator.mdx b/docs-website/docs/pipeline-components/generators/mockchatgenerator.mdx new file mode 100644 index 00000000000..123744a90bc --- /dev/null +++ b/docs-website/docs/pipeline-components/generators/mockchatgenerator.mdx @@ -0,0 +1,109 @@ +--- +title: "MockChatGenerator" +id: mockchatgenerator +slug: "/mockchatgenerator" +description: "A Chat Generator that returns predefined responses without calling any API, for tests and quick prototypes." +--- + +# MockChatGenerator + +A Chat Generator that returns predefined responses without calling any API, for tests and quick prototypes. + +