Tools¶
Agent tool system.
Provides the @tool decorator and tool-related types for registering Python functions as tools that can be called by LLMs.
- agent.tools.tool(func=None, *, name=None, description=None, timeout=None, max_retries=0)[source]¶
- Overloads:
func (Callable[…, Any]) → Tool
func (None), name (str | None), description (str | None), timeout (float | None), max_retries (int) → Callable[[Callable[…, Any]], Tool]
- Parameters:
- Return type:
Decorator to register a function as a tool.
- Usage:
@tool def search(query: str) -> str:
‘’’Search for information.’’’ return f”Results for: {query}”
@tool(name=”custom_name”, timeout=30.0) def fetch_data(url: str) -> str:
‘’’Fetch data from a URL.’’’ …
- class agent.tools.Tool(spec, function, is_async=False, timeout=None, max_retries=0)[source]¶
Bases:
objectA registered tool with its specification and callable function.
- Parameters:
- class agent.tools.ToolSpec(**data)[source]¶
Bases:
BaseModelSpecification for a tool that can be called by an LLM.
- Parameters:
- class agent.tools.ToolCall(**data)[source]¶
Bases:
BaseModelA tool call requested by the LLM.
Agent structured output schemas.
Support for Pydantic-based schema definition and JSON Schema.
- class agent.schemas.Schema(schema, *, strict=True, repair_attempts=1)[source]¶
Bases:
objectWrapper for schema definitions.
Supports both Pydantic models and raw JSON Schema dictionaries.
- agent.schemas.extract_json(text)[source]¶
Extract JSON from text that may contain other content.
Handles: - Plain JSON - JSON in markdown code blocks - JSON embedded in text
- agent.schemas.repair_json(text, error)[source]¶
Attempt to repair malformed JSON.
Basic repairs: - Add missing closing braces/brackets - Fix trailing commas - Fix unquoted keys
Tool execution loop.
Handles the cycle of LLM response -> tool execution -> continue until done.
- class agent.execution.tool_loop.ToolLoopConfig(**data)[source]¶
Bases:
BaseModelConfiguration for tool loop behavior.
- Parameters:
- class agent.execution.tool_loop.ToolLoop(tools, config=None)[source]¶
Bases:
objectManages the tool calling loop.
Executes tools requested by the LLM and continues the conversation until the LLM produces a final response without tool calls.
- Parameters:
config (
ToolLoopConfig|None)
- execute_tool_calls(tool_calls)[source]¶
Execute a list of tool calls synchronously.
- Parameters:
- Return type:
- Returns:
List of tool results
- async execute_tool_calls_async(tool_calls)[source]¶
Execute a list of tool calls asynchronously.
- Parameters:
- Return type:
- Returns:
List of tool results
- build_tool_messages(response, results)[source]¶
Build messages to append to conversation after tool execution.
- Parameters:
response (
AgentResponse) – The LLM response containing tool callsresults (
list[ToolResult]) – Results from tool execution
- Return type:
- Returns:
List of messages to append
- run_loop(initial_request, run_fn)[source]¶
Run the tool loop until completion.
- Parameters:
initial_request (
AgentRequest) – The initial requestrun_fn (
Callable[[AgentRequest],AgentResponse]) – Function to call the LLM
- Return type:
- Returns:
Final response after tool loop completion
- async run_loop_async(initial_request, run_fn)[source]¶
Run the tool loop asynchronously until completion.
- Parameters:
initial_request (
AgentRequest) – The initial requestrun_fn (
Callable[[AgentRequest],Awaitable[AgentResponse]]) – Async function to call the LLM
- Return type:
- Returns:
Final response after tool loop completion
Main execution runtime.
Orchestrates the complete request lifecycle including middleware, retries, tool loops, and structured output handling.
- class agent.execution.runtime.ExecutionRuntime(provider, config, tools=None, middleware=None, retry_config=None, tool_loop_config=None)[source]¶
Bases:
objectCore execution runtime for Agent.
Handles the complete lifecycle of a request: 1. Middleware preprocessing 2. Request preparation 3. Retry handling 4. Tool loop orchestration 5. Structured output parsing 6. Middleware postprocessing
- Parameters:
provider (
BaseProvider)config (
AgentConfig)middleware (
MiddlewareChain|None)retry_config (
RetryConfig|None)tool_loop_config (
ToolLoopConfig|None)
- run(request, schema=None)[source]¶
Execute a request synchronously.
- Parameters:
- Return type:
- Returns:
Normalized response
- async run_async(request, schema=None)[source]¶
Execute a request asynchronously.
- Parameters:
- Return type:
- Returns:
Normalized response
- stream(request)[source]¶
Execute a streaming request.
- Parameters:
request (
AgentRequest) – The normalized request- Return type:
- Returns:
Stream response iterator
- async stream_async(request)[source]¶
Execute an async streaming request.
- Parameters:
request (
AgentRequest) – The normalized request- Return type:
- Returns:
Async stream response iterator