What are Agents?
Agents are autonomous execution units that orchestrate LLM interactions with tool calling. Without tools, an agent functions as a basic LLM. The simplest agent requires just a name and model:Reading guide
The sidebar follows a basics → platform → production path. You can jump anywhere, but this order matches how most agents are built:- Tools — give the agent capabilities (the core primitive)
- Structured output — constrain what comes back
- Memory → Memory compaction — multi-turn context, then keeping oversized tool results and history within the window
- Skills — domain packages (knowledge + tools) once the agent loop makes sense
- Dynamic agents — runtime prompts and tool sets
- Background tasks · Commands — utilities (async tools, slash shortcuts)
suspend(), and durable resume (agents, workflow steps, and tools).
For live spoken conversations, continue to Voice agents for speech providers, browser and phone transports, interruptions, and usage tracking.
This page covers running agents, models, and I/O. The sections below are the reference for that; the linked pages go deeper on each topic.
Model Providers
You can specify any model using the “provider/model” format. See all supported models in the Model Reference. Some models require specific parameters (likemax_tokens for Claude). Pass it as a top-level field:
.env file.
Fallback Models
UseFallbackModel when you want an agent to try another model if the primary provider fails, including authentication errors, rate limits, timeouts, and server errors.
FallbackExhausted with the per-model errors.
For per-model settings, use ModelEntry:
api_key and base_url apply only to models from the primary provider. When the chain switches providers, Timbal resolves that provider’s own credentials and endpoint from its environment variables or the platform proxy. Use ModelEntry(api_key=..., base_url=...) to override these for an individual model, including when multiple providers share a gateway.
The agent’s model_params (or per-request provider_params) also apply only to models from the primary provider. Other providers use their defaults unless their ModelEntry sets provider_params. An entry’s dict replaces, rather than merges with, the shared params; use {} to clear them explicitly. An omitted value or None inherits shared params only for the primary provider. Timbal does not translate provider-specific options such as OpenAI’s reasoning into Anthropic’s thinking. This scoping also applies to providers that share an API format, such as OpenAI and xAI; set entry-level params explicitly to reuse compatible options. Shared max_tokens, temperature, tools, and structured output configuration still apply across the chain.
Thinking and reasoning
Some models support extended thinking before responding. Check per-model capabilities on the Model Reference pages. Configure at construction time viamodel_params (per-request overrides use provider_params — see Overriding Model Configuration below):
Running Agents
Execute agents by calling them with aprompt parameter and using .collect() to get the result:
Streaming Events
For real-time processing, you can stream events as they happen:Iteration Budget
max_iter (default 10) bounds the LLM → tool rounds in one turn. on_max_iter decides what happens when the budget is spent and the model still wants tools:
The notice matters: silently handed an empty tool list, models narrate the step they were about to take (“I’ll update the file now”) as if it had happened.
For anything custom, pass a callable (sync or async). It receives a
MaxIterContext(max_iter, memory, agent_path) and returns a message: role="user" is used as the notice for a final tool-less call, role="assistant" is the reply and ends the turn, None makes the tool-less call with no notice, and raising fails the run.
metadata["max_iter_reached"] = {"max_iter", "behavior"} on the agent span. Injected messages are persisted in memory tagged metadata={"source": "runtime", "kind": "max_iter_notice" | "max_iter_stop"}, so Message.is_runtime() lets you hide them when rendering a human transcript.
Timeout
max_iter bounds steps; timeout bounds time. It is the wall-clock budget, in seconds, for one turn — every LLM call and tool call included. Default None (no limit).
status.code == "timeout" and an error of type RunTimeout. Memory stays consistent: an interrupted tool call gets a synthetic result, so the next turn runs normally.
timeout is available on every runnable, not just agents:
- Sub-agents and tools. A child’s own
timeoutfails only that call. The parent agent receives it as an error tool result and can recover. Atimeouton the parent bounds the whole turn, children included. - Workflow steps. A step that times out blocks its dependents, including transitive dependents. Unrelated branches may finish before the workflow reports the step failure. An agent inside a workflow can still recover from its own tools’ timeouts; the workflow uses that agent’s final result. See Errors & failure.
- Hooks.
pre_hook, the handler, andpost_hookshare one deadline. A slow hook times out the call; the budget is not restarted between stages. If the handler finished before a post-hook timed out, its output is retained. - What doesn’t count. The clock starts after input validation and the approval gate, so time a run spends paused waiting for approval does not use up the budget. Children detached with
background_modeusebackground_timeoutinstead (see Background Tasks). - Sync handlers. A sync tool running inline on the event loop can’t be interrupted. Make it async, or set
offload_blocking=True.
TimeoutError raised inside a handler, such as a slow HTTP call, is still reported as a regular error. Only the runnable’s own deadline produces timeout.
Performance. An unset timeout has negligible overhead: the per-call check remains, but the stream relay and timers are bypassed. Enabling a timeout adds timer and relay work per streamed event. The benchmarks/timeout/bench_streaming.py benchmark measures this cost separately from network and LLM latency.
Approval-Required Tools
Any runnable — Tool, Agent, or Workflow step — can pause for human approval before it runs. Mark it withrequires_approval, listen for ApprovalEvent, and resume by calling the runnable again with resume:
pending_approvals(), status reasons, usage counters, plus suspend() for asking the user mid-run — lives on the dedicated Human in the Loop section.
Input
Agents communicate throughMessage objects - Timbal’s data structure that standardizes both input and output.
Message objects:
Passing Input Parameters
You can pass custom input parameters when calling an agent. Access these values anywhere in your agent (tools, system prompts, hooks) usingget_run_context().current_span().input:
user_id and role from the input parameters. Input parameters work with both .collect() and streaming.
For more information about accessing input parameters and using the run context, see the Context & State Management page.
Overriding Model Configuration
What if you want to change the model,max_tokens, or thinking config for each run? Instead of creating multiple agents, you can pass these as input parameters. This is useful for A/B testing different models, adjusting token limits per request, or dynamically selecting models based on task complexity.
Output
Calling.collect() returns an OutputEvent containing the agent’s response. Access the Message via the .output property:
Learn more about events in Events & Streaming.
Messages
Messages are the structured data format that agents use to communicate. They contain a role and content, with automatic handling of different content types and provider compatibility.- user - Messages from the user
- assistant - Messages from the AI agent
- system - System instructions and context
- tool - Tool execution results
- TextContent - Plain text messages
- FileContent - Files like PDFs, images, documents
- ToolUseContent - Function calls to tools
- ToolResultContent - Results from tool executions
Message.validate():
Files
Agents can process files directly through the message content system. The framework automatically handles file reading, content extraction, and formatting for the AI model.- Text files (.txt, .md) - Direct content inclusion
- PDFs (.pdf) - Text extraction with structure preservation
- Images (.png, .jpg, .gif) - Visual analysis through vision-capable models
- Spreadsheets (.xlsx, .csv) - Structured data representation
- Documents (.docx) - Text and formatting extraction
File objects using File.validate():