query in the schema. The agent automatically fills state before calling the tool.
Quick Start
Simplest form — bare Injected
Injected behaves like Injected[dict] — excluded from the tool schema and resolved to state.to_dict().Create a tool with injected state
state parameter never appears in tool descriptions shown to the LLM — only query does.How It Works
When a tool parameter is annotated withInjected, Injected[T], or bare Injected, the SDK:
- Strips that parameter from the schema sent to the LLM
- Captures the current
AgentStateat call time - Injects the state value automatically before the tool runs
| Annotation | Injected value |
|---|---|
Injected | state.to_dict() — same as Injected[dict] |
Injected[dict] | state.to_dict() — plain Python dict |
Injected[AgentState] | Full AgentState dataclass with all fields |
Configuration Options
AgentState fields available at runtime:
| Field | Type | Description |
|---|---|---|
agent_id | str | Name/ID of the running agent |
run_id | str | Unique ID for this run |
session_id | str | Conversation session identifier |
last_user_message | str | None | Most recent message from the user |
last_agent_message | str | None | Most recent message from the agent |
memory | Any | Agent memory object if configured |
previous_tool_results | list | Results from earlier tool calls in this run |
metadata | dict | Extra key-value context |
Injected Tools TypeScript Reference
TypeScript injected state configuration
Tools Rust Reference
Rust tool configuration
Common Patterns
Multi-turn memory access in a tool:Best Practices
Prefer bare Injected or Injected[dict] for simple access
Prefer bare Injected or Injected[dict] for simple access
Bare
Injected and Injected[dict] both return state.to_dict(). Use Injected[AgentState] when you need memory, learn_manager, or previous_tool_results objects directly.Never add injected params to tool docstrings
Never add injected params to tool docstrings
The LLM builds its understanding from the docstring. Documenting injected parameters confuses the model — describe only the user-facing parameters.
Use with_injection_context in tests
Use with_injection_context in tests
Direct unit tests cannot rely on an agent to set up injection context. Wrap calls with
with_injection_context(state) to test tools in isolation without spinning up a full agent.Keep injected parameters optional-safe
Keep injected parameters optional-safe
get_current_state() returns None when called outside an agent context. resolve_injected_value falls back to an empty dict. Design tools to handle empty state gracefully.Related
Tools
Creating and registering tools with agents
Middleware
Intercept tool calls before and after execution

