Overview
TheAgent class is the core component of the Swarms framework, connecting LLMs with tools, long-term memory, and advanced autonomous capabilities. It provides a production-ready interface for building intelligent agents that can reason, use tools, handle multimodal inputs, and execute complex tasks. The class is designed to handle a variety of document types—including PDFs, text files, Markdown, and JSON—enabling robust document ingestion and processing.
Import
Key Features
- Tool Integration: Native support for function calling and tool execution
- Long-term Memory: RAG-based memory system for context retention
- Autonomous Loops: Dynamic execution with configurable stopping conditions
- Multi-modal Support: Process text, images, and other media
- MCP Support: Integration with Model Context Protocol servers
- Agent Handoffs: Delegate tasks to specialized agents
- Streaming: Real-time token streaming with callbacks
- Fallback Models: Automatic failover to backup models
- State Management: Autosave and state persistence
- Telemetry: Optional OpenTelemetry tracing of every run
Architecture
Agent delegates its larger subsystems to dedicated collaborator objects, each built during __init__ and reachable as an attribute. The agent’s own methods are thin wrappers over them, so both call styles work and neither is deprecated.
Initialization
Optional[str]
default:"None"
Unique identifier for the agent instance. When omitted the agent generates one via
generate_id("agent"), of the form agent- followed by 32 hex characters (secrets.token_hex(16)) — not a UUID.str
default:"swarm-worker-01"
The name of the agent, used for identification and logging. Omit it and the
agent defaults to the literal
"swarm-worker-01" — every agent that does
not set a name shares this same value.Because the default is a shared literal rather than a generated value,
anything keyed on
agent_name collides across unnamed agents: concurrent
result dictionaries keep one entry for the whole group, and MEMORY.md
folders are shared. Pass an explicit name whenever agents run
concurrently or need memory to be stable across restarts.str
A description of the agent’s purpose and capabilities. Shown to orchestrators when routing tasks.
Optional[str]
default:"AGENT_SYSTEM_PROMPT_3"
The system prompt that defines the agent’s behavior and personality. Defaults to the framework’s general-purpose
AGENT_SYSTEM_PROMPT_3 rather than an empty prompt.Any
The language model instance to use. If None, a LiteLLM instance will be created
str
default:"gpt-5.4"
The LiteLLM-compatible model identifier (e.g.
"gpt-5.4", "claude-sonnet-4-6", "groq/llama-3.3-70b-versatile").dict
default:"None"
Extra keyword arguments forwarded to the underlying LiteLLM client.
bool
default:"False"
Enable provider-side prompt caching. When
True, ephemeral cache_control breakpoints are added to the stable prefix of each request (system prompt, tools, and the last message) so it is cached and re-billed at a large discount. Applies to the Anthropic model family (Claude on Anthropic / Bedrock / Vertex); providers that cache automatically (e.g. OpenAI) are left untouched. See the Prompt Caching guide.dict
default:"None"
Fine-grained prompt-caching options; only consulted when
prompt_caching=True. All keys optional:ttl(str):"5m"(default) or"1h"for Anthropic’s extended cache (2x write cost, survives longer gaps; the required beta header is attached automatically).cache_system_prompt(bool, defaultTrue): cache the system prefix.cache_messages(bool, defaultTrue): cache through the last message (incremental multi-turn caching).cache_tools(bool, defaultTrue): cache the tool-definitions block.override(bool, defaultNone): forcecache_controlinjection on/off regardless of the detected provider (e.g. opt Gemini/Vertex in, or a custom alias out).Noneauto-detects (Anthropic only).prompt_cache_key(str): OpenAI-only routing hint for higher cache hit rates.prompt_cache_retention(str): OpenAI-only cache TTL —"in_memory"or"24h".
str
default:"None"
Base URL for OpenAI-compatible providers (Ollama, LM Studio, vLLM, etc.).
str
default:"None"
Override API key for the LLM provider. Falls back to environment variables when unset.
str
default:"None"
Single fallback model used when the primary model fails.
Optional[Union[int, str]]
default:"1"
Maximum number of reasoning loops. Use “auto” for autonomous mode with dynamic planning
List[Callable]
List of callable functions that the agent can use as tools
float
default:"0.5"
Temperature for LLM sampling (0.0 to 1.0)
Optional[int]
default:"None"
Maximum number of tokens in the LLM response. A positive value you pass is kept and forwarded to the LLM. Leave it as
None (or pass 0/a negative number) and the agent resolves the model’s own maximum output-token limit, falling back to 16000 when that lookup fails.int
default:"None"
Effective context window in tokens. When
context_compression=True, the agent compresses memory once usage crosses 90% of this limit. Leave as None to derive the window from the model.float
default:"None"
Nucleus-sampling parameter. Stripped automatically for Anthropic models when extended thinking is enabled.
bool
default:"True"
Allow the framework to grow/shrink the per-call context budget based on token usage signals.
bool
default:"True"
When
True, the agent runs a ContextCompressor that summarises long histories at 90% of context_length so long sessions never hit the context wall.bool
default:"False"
When
True, read/write MEMORY.md under the workspace so agent state survives process restarts. Off by default: opt in explicitly for agents that should remember across runs.Union[TransformConfig, dict]
default:"None"
Optional pre/post-processing transforms applied to the conversation history.
bool
default:"False"
Enable basic streaming with formatted panels
bool
default:"False"
Enable detailed token-by-token streaming with metadata (citations, tokens used, etc.)
Callable[[str], None]
Callback function to receive streaming tokens in real-time. Use with
agent.run_stream / agent.arun_stream for generator-style consumption.bool
default:"False"
Enable interactive mode (REPL-style) — prompt the user for input between loops.
bool
default:"False"
Enable verbose logging for debugging.
bool
default:"True"
When
False, suppress the agent’s printed output (Rich panels, thinking panel, etc.). Token streams via arun_stream / streaming_callback are unaffected.OutputType
default:"str-all-except-first"
How the run’s result is formatted. See Output Types for all 17 accepted values.
bool
default:"False"
Automatically save agent state during execution
bool
default:"False"
Display agent dashboard on initialization
Optional[Union[Callable, Any]]
default:"None"
A store that is written out with agent state. The agent never queries it — only
.save(path) is ever called on it, so this does not provide RAG. For retrieval, expose the lookup as a tool instead.List[str]
List of fallback models to try in order if the primary model fails.
Optional[int]
default:"3"
Number of retry attempts for LLM calls
str
Token that signals the agent to stop execution
Callable[[str], bool]
Function that returns True when the agent should stop
Callable
Alternative stopping function
bool
default:"False"
Enable dynamic temperature adjustment during execution
bool
default:"False"
Enable dynamic loop count adjustment (sets max_loops=“auto”)
int
default:"0"
Seconds to wait between consecutive loop iterations.
str
default:"exit"
Token the user can type in interactive mode to exit the loop.
bool
default:"False"
When
True, append the framework’s preset stopping marker to the system prompt.bool
default:"False"
Auto-generate a system prompt from the task description when one is not provided.
str
default:"Human"
Name of the user in conversation history
str
default:"Auto-generated"
Path that
save() writes to when called with no argument. When unset, falls back to an auto-generated {api_key}_state.json filename inside the agent workspace.str
Standard operating procedure for the agent
List[str]
List of standard operating procedures
str
Rules that govern agent behavior
str
Prompt for planning phase
bool
default:"False"
Enable planning phase before execution
Optional[bool]
default:"None"
Enable multi-modal processing (images, etc.).
bool
default:"True"
After every tool call, run a brief LLM summary of the tool result and add it to the conversation.
int
default:"3"
Number of times to retry a failing tool call before giving up.
bool
default:"True"
Display tool inputs/outputs in the agent’s printed output.
Optional[List[Dict[str, Any]]]
default:"None"
Pre-built OpenAI function-calling tool schemas. Use when you want to bypass the auto-generated schema.
ToolUsageType
default:"None"
Override tool schema used at runtime.
Callable
default:"None"
Optional post-processor applied to the agent’s output before returning.
List[BaseModel]
default:"None"
Pydantic models registered for structured-output prompting.
Optional[Union[str, MCPConnection, Dict]]
default:"None"
A single MCP server. Pass a URL string for an unauthenticated server, or an
MCPConnection/dict to configure auth, transport, headers and timeouts.Optional[List[Union[str, MCPConnection, Dict]]]
default:"None"
Several MCP servers. Each entry may be a URL string, an
MCPConnection, or a dict. Tools from every server are merged and each tool call is routed back to the server that owns it.Optional[Union[MCPConnection, Dict]]
default:"None"
A single MCP server given as a connection object (or the equivalent dict).
Optional[List[Union[MCPConnection, Dict]]]
default:"None"
Several MCP servers given as connection objects (or dicts).
Optional[str]
default:"None"
API key applied to every MCP server that does not define its own. Sent as
Authorization: Bearer <key> by default; override the header or prefix per-server with MCPConnection(api_key_header=..., api_key_prefix=...). Supports "env:MY_VAR" / "${MY_VAR}" indirection so secrets stay out of code.Optional[str]
default:"None"
Bearer token applied to every MCP server that does not define its own. Equivalent to
mcp_api_key with the default header and prefix.Optional[Union[MCPOAuthConfig, Dict]]
default:"None"
OAuth 2.1 settings applied to every MCP server without its own. Supports the interactive authorization-code flow (PKCE plus dynamic client registration, tokens cached on disk), the headless
client_credentials grant, and pre-issued access tokens.Optional[Dict[str, str]]
default:"None"
Extra headers merged into every MCP request.
Optional[Literal['streamable_http', 'sse', 'stdio', 'auto']]
default:"None"
Force a transport for every MCP server.
None auto-detects from the URL.Optional[int]
default:"None"
Request timeout in seconds for every MCP server. Falls back to the per-connection default of 30.
Union[Sequence[Callable], Any]
List of agents to enable task handoffs/delegation
List[str]
Free-form list of agent capabilities used for routing and documentation.
agent_roles
default:"worker"
The agent’s role within a swarm (e.g.
"worker", "director").List[str]
default:"None"
Tags used to filter or categorise the agent.
List[Dict[str, Any]]
default:"None"
Structured list of intended use cases for documentation/marketplace listings.
Literal['interactive', 'fast', 'standard']
default:"standard"
Execution mode:
interactive (REPL), fast (minimal logging/decoration), or standard.str
UUID of a prompt from the Swarms marketplace to use as the system prompt.
bool
default:"False"
When
True, publish this agent to the Swarms marketplace on initialization.str
Path to a directory of Agent Skills (Anthropic
SKILL.md format).Optional[Union[str, List[str]]]
default:"all"
Tools to enable for the autonomous looper when
max_loops="auto". Use "all", or a list of tool names — the list filters the entire loop tool set, control-flow tools included, so include create_plan and complete_task unless you intend to remove them. agent.get_all_selected_tools() returns every valid name.bool
default:"False"
Give the autonomous looper (
max_loops="auto") a think tool for an explicit reasoning turn. Off by default: it costs a full round-trip to produce reasoning most models can emit inline alongside their actions. When False the system prompt is adjusted to match, so the model is never told to call a tool it does not have.bool
default:"True"
Defer tool schemas behind a
tool_search tool instead of sending them all on every request. See Dynamic Tool Loading below.bool
default:"False"
Enable ReAct-style reasoning prompting.
bool
default:"True"
Whether to prepend the framework’s reasoning preamble to the system prompt.
bool
default:"False"
Enable reasoning mode for supported models (e.g. o1, o3, Claude with extended thinking).
str
default:"None"
Effort level for reasoning models:
"minimal", "low", "medium", "high", "xhigh", "ultra", "max", or "none".Left unset, the parameter is not sent to the provider at all.int
default:"1024"
Maximum extended-thinking budget for Claude reasoning models.
bool
default:"False"
Prepend the framework’s safety preamble to the system prompt.
bool
default:"False"
Randomly select from a pool of models on each call (load-balancing/experimentation).
str
Not a constructor argument — any value passed here is ignored. The workspace root is read from the
WORKSPACE_DIR environment variable; set it explicitly (e.g. via a .env file — .env.example uses agent_workspace). When it is unset, agent.workspace_dir resolves to None and an error is logged; the workspace manager behind agent.workspace then falls back to {cwd}/agent_workspace the first time it needs a directory. Each agent gets its own subdirectory at {workspace}/agents/{agent-name}-{id12}/, where id12 is the last 12 characters of agent.id. Read the resolved path from agent.workspace.dir.str
default:"None"
Path from which to load saved agent state on init.
Methods
run
Execute the agent’s main loop for a given task.Union[str, Any]
The task or prompt for the agent to process
str
Optional image path or data for vision-enabled models
List[str]
Optional list of image paths for batch processing
str
Expected correct answer for validation with automatic retries
Callable[[str], None]
Callback function to receive streaming tokens in real-time
int
default:"1"
Number of times to run the task. When
n > 1, run recursively calls itself n times and returns a list of results.List[Dict[str, Any]]
Prior conversation as typed chat turns. When given, these replace the transcript
the agent would otherwise derive from its own memory, and
task is appended as
the new user turn. Multi-agent structures use this to hand an agent the shared
room with roles intact — the agent’s own turns as assistant, everyone else’s
as labelled user turns — instead of one flattened string.Any
Agent output formatted according to output_type configuration
Examples:
call
Alternative syntax for running the agent (callsrun internally).
arun
Async version ofrun.
run_batched
Run multiple tasks sequentially, one after another, and collect the results. For concurrent execution userun_concurrent_tasks instead.
List[str]
List of tasks to run, in order
List[str]
default:"None"
One image per task, paired by position. Omit to run the tasks without images. A length mismatch raises
ValueError rather than silently dropping the extras.List[Any]
List of results from each task execution, in the same order as the input tasks
run_stream
Run the agent and yield response tokens one-by-one as a sync generator. The full agent loop (multi-step reasoning, tool calls, MCP, autonomous plan/execute/summary) runs in a background daemon thread; tokens are forwarded to the caller the moment the LLM emits them.arun_stream
Async generator version ofrun_stream. The agent loop runs in a thread executor while tokens are forwarded through an asyncio.Queue, so the caller’s event loop is never blocked.
Both
run_stream and arun_stream work for any max_loops value (1, integer > 1 with tools, or "auto"). They stream tokens through every internal loop, including tool-call turns, synthesis turns after a tool returns, and the autonomous plan/execute/summary cycle.run_concurrent_tasks
Run a batch of tasks concurrently via a thread pool.bulk_run
Generate responses for multiple input sets. Each input is a dict of kwargs forwarded torun.
save
Save the agent’s current state to disk.load
Load agent state from a saved file (JSON, viaSafeStateManager). If file_path is omitted, falls back to load_state_path, then saved_state_path, then a path derived from agent_name.
save_to_yaml
Save the agent to a YAML file.to_dict
Convert agent configuration to dictionary.to_json
Convert agent configuration to JSON string.to_yaml
Convert agent configuration to YAML string.to_toml
Convert agent configuration to TOML string.model_dump_json / model_dump_yaml
Save the agent model to a JSON or YAML file in the workspace directory.add_tool / add_tools
Dynamically add a tool (or list of tools) to the agent at runtime.remove_tool / remove_tools
Remove a previously-registered tool (or list of tools).add_memory
Append a message to the agent’s short-term memory.talk_to
Initiate a conversation with another agent.talk_to_multiple_agents
Talk to multiple agents concurrently.None.
receive_message / send_agent_message
receive_message wraps an incoming message from another agent in a short preamble and runs it through the agent’s normal run() loop, returning the agent’s response. send_agent_message prefixes a message with To: {agent_name}: and runs that through run(), returning the result.
Task delegation via
handoffs happens automatically inside run() — the LLM calls an internal handoff tool when it decides to delegate. There is no separate public handle_handoffs() method to call directly.reset
Drop the agent’s short-term memory by settingagent.short_memory = None. Nothing is re-initialized in its place.
plan
Run only the planning phase for a task without executing.print_dashboard
Display the agent’s configuration dashboard.showcase_config
Display the agent’s configuration in a formatted table.update_system_prompt / update_max_loops / update_loop_interval
In-place setters for runtime reconfiguration.Tool Management
Methods backing dynamic tool loading and MCP tool discovery. See Dynamic Tool Loading.
Two related properties:
get_llm_parameters
Returns the parameters of the language model as a string (str(vars(self.llm))).
Fallback Model Helpers
Methods backing thefallback_models / fallback_model_name feature. All delegate to LLMManager.
Skills Helpers
Agent Skills loading, delegating toSkillsManager.
agent.skills_dir and agent.skills_metadata are properties that read and write through to the manager.
Marketplace Helpers
Marketplace integration, delegating toAgentMarketplaceHandler.
marketplace_prompt_id loads a prompt during construction; setting publish_to_marketplace=True publishes during construction.
Complete Methods Reference
Advanced Capabilities
Tool Integration
TheAgent class allows seamless integration of external tools by accepting a list of Python functions via the tools parameter. Each tool function must include type annotations and a docstring. The agent automatically converts these functions into an OpenAI-compatible function calling schema.
tools_list_dictionary:
Dynamic Tool Loading
dynamic_tools=True is the default, and it changes how tools reach the model.
Tool definitions are re-sent on every request, so a large tool set is paid for on every turn. When dynamic_tools is on and the agent has something to defer — that is, when tools are given, an MCP server is configured, or max_loops="auto" — the agent:
- Appends a notice to the system prompt telling the model that most of its tools are not currently loaded.
- Sends only a
tool_searchtool (plus anything marked always-loaded, such as the autonomous loop’s control-flow tools) instead of the full schema list. - Loads the schemas the model asks for through
tool_search. They become callable on the next turn, not the one that searched.
max_loops has nothing to defer, so nothing changes.
Deferral is a separate mechanism from
selected_tools. selected_tools decides which autonomous-loop tools exist at all; dynamic_tools decides which of the existing schemas are sent up front.External Knowledge and Retrieval
Swarms bundles no vector database, andAgent performs no retrieval of its own. To give an agent access to an external knowledge base, expose the lookup as a tool — the agent then decides when to query it and the result enters the conversation like any other tool output.
Memory Persistence and Context Compression
The agent ships with two complementary memory controls that work together to manage what is remembered between runs and how the context window is managed during long sessions.Persistent Memory
Withpersistent_memory=True the agent reads and writes a MEMORY.md file under $WORKSPACE_DIR/agents/{agent_name}/MEMORY.md. Every message is appended to this file, and on the next run the file is loaded back so the agent remembers prior interactions.
The default is persistent_memory=False — no on-disk state at all, so the agent starts from a blank slate every run. Opt in explicitly for agents that should remember across runs, and use the same agent_name each time, since the memory file is keyed on it.
Context Compression
For long-running agents the conversation history can grow until it fills the context window.context_compression=True (the default) attaches a ContextCompressor that automatically summarises and collapses MEMORY.md whenever token usage crosses 90% of context_length.
Combining Both Controls
Agent Handoffs and Task Delegation
TheAgent class supports intelligent task delegation through the handoffs parameter. When provided with a list of specialized agents, the main agent acts as a router that analyzes incoming tasks and delegates them to the most appropriate specialized agent.
How Handoffs Work:
- Task Analysis: When a task is received, the main agent uses a built-in “boss agent” to analyze the task requirements
- Agent Selection: The boss agent evaluates all available specialized agents and selects the most suitable one(s)
- Task Delegation: The selected agent(s) receive the task and process it
- Response Aggregation: Results from specialized agents are collected and returned
Autonomous Mode
Whenmax_loops="auto" is set, the agent enables automatic planning and execution. The agent creates a structured plan with subtasks, executes them sequentially with dependency management, and generates a comprehensive summary.
Available Tools in Autonomous Mode
Whenmax_loops="auto" and interactive=False, the agent has access to specialized tools:
think is opt-in. It is stripped from the tool list unless the agent is constructed with think_tool=True, and the system prompt is adjusted to match so the model is never told to call a tool it lacks.$WORKSPACE_DIR/agents/{agent-name}-{id12}/, reachable as agent.workspace.dir).
Sub-Agent Delegation
The autonomous agent can create and manage sub-agents for parallel task execution:Batch Processing
Run a list of tasks one after another withrun_batched. It is sequential, not concurrent — reach for run_concurrent_tasks when the tasks are independent and you want them in parallel:
Examples
Basic Usage
Minimal Configuration
Agent with Tools
Multi-modal Agent
Multi-Image Processing
Autonomous Agent with Auto Loops
Multiple Loops
Dynamic Loops
Agent with Streaming
Token-by-Token Streaming
Agent with Fallback Models
Agent with MCP Integration
Multiple MCP Connections
MCP with Connection Config
Agent Handoffs
Interactive Mode
Auto Generate Prompt
Reasoning-Enabled Models
Execution Modes
Marketplace Prompt Loading
Publishing to Marketplace
Message Transforms for Context Management
Agent with Capabilities
Saving and Loading State
Autosave
Whenautosave=True, the agent saves its configuration at each loop step to {workspace}/agents/{agent-name}-{id12}/config.json via its WorkspaceManager. Files are written atomically to prevent corruption.
WORKSPACE_DIR environment variable. Autosave goes through WorkspaceManager, which falls back to {cwd}/agent_workspace (and sets WORKSPACE_DIR to it) when the variable is unset. Other readers are stricter: agent.workspace_dir, resolved during construction, is None when the variable was unset at that moment. Set WORKSPACE_DIR explicitly rather than relying on the fallback.
Async and Concurrent Execution
Comprehensive Agent Configuration
Various Settings
Output Types
The agent supports multiple output formats via theoutput_type parameter:
The accepted values are the
HistoryOutputType literal in swarms.utils.output_types; anything outside it raises ValueError.
Error Handling
The Agent class includes comprehensive error handling:- AgentError: Base class for every exception below
- AgentInitializationError: Raised when agent fails to initialize
- AgentRunError: Raised when execution fails
- AgentLLMError: Raised when LLM encounters issues
- AgentLLMInitializationError: Raised when the LLM fails to initialize
- AgentToolExecutionError: Raised when the agent fails to execute a tool
swarms.schemas.agent_errors so collaborator classes can raise and catch them without importing Agent:
swarms.schemas.agent_mcp_errors: AgentMCPError, AgentMCPConnectionError, AgentMCPToolError.
Telemetry
Agent.run is instrumented with OpenTelemetry, on by default. Each run emits an Agent.run span carrying the task, the output, status, and the agent’s identity — nested under the swarm run that invoked it, if any. Construction emits an Agent.init span carrying the full constructor configuration.
Opt out with SWARMS_TELEMETRY_ON=false, which reduces the cost to ~0.14 µs per call. See the Telemetry guide for what is captured and how to turn it off.
New Features and Parameters
Enhanced Run Method Parameters
imgs: Process multiple images simultaneously instead of just onecorrect_answer: Validate responses against expected answers with automatic retriesstreaming_callback: Real-time token streaming for interactive applications
MCP (Model Context Protocol) Integration
Advanced Reasoning and Safety
Performance and Resource Management
Advanced Memory and Context
Enhanced Tool Management
Advanced LLM Configuration
Execution Modes and Marketplace
Best Practices
Related
Agent subsystems- LLMManager - Model selection, invocation, and fallback rotation
- MCPManager - MCP servers, tool discovery, and tool-call routing
- SkillsManager - Agent Skills loading and prompt rendering
- AgentMarketplaceHandler - Marketplace fetch and publish