Skip to main content

Overview

The Agent 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

Initialization

id
str
default:"agent-{uuid}"
Unique identifier for the agent instance
agent_name
str
default:"swarm-worker-01"
The name of the agent, used for identification and logging
agent_description
str
A description of the agent’s purpose and capabilities. Shown to orchestrators when routing tasks.
system_prompt
str
The system prompt that defines the agent’s behavior and personality
llm
Any
The language model instance to use. If None, a LiteLLM instance will be created
model_name
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").
llm_args
dict
default:"None"
Extra keyword arguments forwarded to the underlying LiteLLM client.
prompt_caching
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.
cache_config
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, default True): cache the system prefix.
  • cache_messages (bool, default True): cache through the last message (incremental multi-turn caching).
  • cache_tools (bool, default True): cache the tool-definitions block.
  • override (bool, default None): force cache_control injection on/off regardless of the detected provider (e.g. opt Gemini/Vertex in, or a custom alias out). None auto-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".
llm_base_url
str
default:"None"
Base URL for OpenAI-compatible providers (Ollama, LM Studio, vLLM, etc.).
llm_api_key
str
default:"None"
Override API key for the LLM provider. Falls back to environment variables when unset.
fallback_model_name
str
default:"None"
Single fallback model used when the primary model fails.
max_loops
Union[int, str]
default:"1"
Maximum number of reasoning loops. Use “auto” for autonomous mode with dynamic planning
tools
List[Callable]
List of callable functions that the agent can use as tools
temperature
float
default:"0.5"
Temperature for LLM sampling (0.0 to 1.0)
max_tokens
int
default:"4096"
Maximum number of tokens in the LLM response
context_length
int
default:"16000"
Effective context window in tokens. When context_compression=True, the agent compresses memory once usage crosses 90% of this limit.
top_p
float
default:"None"
Nucleus-sampling parameter. Stripped automatically for Anthropic models when extended thinking is enabled.
dynamic_context_window
bool
default:"True"
Allow the framework to grow/shrink the per-call context budget based on token usage signals.
context_compression
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.
persistent_memory
bool
default:"True"
When True, read/write MEMORY.md under the workspace so agent state survives process restarts. Set False for stateless tasks.
transforms
Union[TransformConfig, dict]
default:"None"
Optional pre/post-processing transforms applied to the conversation history.
streaming_on
bool
default:"false"
Enable basic streaming with formatted panels
stream
bool
default:"false"
Enable detailed token-by-token streaming with metadata (citations, tokens used, etc.)
streaming_callback
Callable[[str], None]
Callback function to receive streaming tokens in real-time. Use with agent.run_stream / agent.arun_stream for generator-style consumption.
interactive
bool
default:"false"
Enable interactive mode (REPL-style) — prompt the user for input between loops.
verbose
bool
default:"false"
Enable verbose logging for debugging.
print_on
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.
output_type
OutputType
default:"str-all-except-first"
Output format: ‘str’, ‘string’, ‘list’, ‘json’, ‘dict’, ‘yaml’, ‘xml’
autosave
bool
default:"false"
Automatically save agent state during execution
dashboard
bool
default:"false"
Display agent dashboard on initialization
long_term_memory
Union[Callable, Any]
Long-term memory backend (e.g. vector database) for RAG.
fallback_models
List[str]
List of fallback models to try in order if the primary model fails.
retry_attempts
int
default:"3"
Number of retry attempts for LLM calls
retry_interval
int
default:"1"
Interval in seconds between retry attempts
stopping_token
str
Token that signals the agent to stop execution
stopping_condition
Callable[[str], bool]
Function that returns True when the agent should stop
stopping_func
Callable
Alternative stopping function
dynamic_temperature_enabled
bool
default:"false"
Enable dynamic temperature adjustment during execution
dynamic_loops
bool
default:"false"
Enable dynamic loop count adjustment (sets max_loops=“auto”)
loop_interval
int
default:"0"
Seconds to wait between consecutive loop iterations.
custom_exit_command
str
default:"exit"
Token the user can type in interactive mode to exit the loop.
preset_stopping_token
bool
default:"false"
When True, append the framework’s preset stopping marker to the system prompt.
auto_generate_prompt
bool
default:"false"
Auto-generate a system prompt from the task description when one is not provided.
user_name
str
default:"Human"
Name of the user in conversation history
saved_state_path
str
Path to save agent state
sop
str
Standard operating procedure for the agent
sop_list
List[str]
List of standard operating procedures
rules
str
Rules that govern agent behavior
planning_prompt
str
Prompt for planning phase
plan_enabled
bool
default:"false"
Enable planning phase before execution
multi_modal
bool
Enable multi-modal processing (images, etc.).
summarize_multiple_images
bool
default:"false"
When multiple images are provided, summarise them into a single context entry before invoking the LLM.
tool_call_summary
bool
default:"True"
After every tool call, run a brief LLM summary of the tool result and add it to the conversation.
tool_retry_attempts
int
default:"3"
Number of times to retry a failing tool call before giving up.
show_tool_execution_output
bool
default:"True"
Display tool inputs/outputs in the agent’s printed output.
tools_list_dictionary
List[Dict[str, Any]]
default:"None"
Pre-built OpenAI function-calling tool schemas. Use when you want to bypass the auto-generated schema.
tool_schema
ToolUsageType
default:"None"
Override tool schema used at runtime.
output_cleaner
Callable
default:"None"
Optional post-processor applied to the agent’s output before returning.
list_base_models
List[BaseModel]
default:"None"
Pydantic models registered for structured-output prompting.
mcp_url
Union[str, MCPConnection]
URL or connection object for a single MCP server.
mcp_urls
List[str]
List of MCP server URLs for connecting to multiple servers.
mcp_config
MCPConnection
Single MCP connection configuration object.
handoffs
Union[Sequence[Callable], Any]
List of agents to enable task handoffs/delegation
capabilities
List[str]
Free-form list of agent capabilities used for routing and documentation.
role
agent_roles
default:"worker"
The agent’s role within a swarm (e.g. "worker", "director").
tags
List[str]
default:"None"
Tags used to filter or categorise the agent.
use_cases
List[Dict[str, Any]]
default:"None"
Structured list of intended use cases for documentation/marketplace listings.
mode
Literal['interactive', 'fast', 'standard']
default:"standard"
Execution mode: interactive (REPL), fast (minimal logging/decoration), or standard.
marketplace_prompt_id
str
UUID of a prompt from the Swarms marketplace to use as the system prompt.
publish_to_marketplace
bool
default:"false"
When True, publish this agent to the Swarms marketplace on initialization.
skills_dir
str
Path to a directory of Agent Skills (Anthropic SKILL.md format).
selected_tools
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.
react_on
bool
default:"false"
Enable ReAct-style reasoning prompting.
reasoning_prompt_on
bool
default:"True"
Whether to prepend the framework’s reasoning preamble to the system prompt.
reasoning_enabled
bool
default:"false"
Enable reasoning mode for supported models (e.g. o1, o3, Claude with extended thinking).
reasoning_effort
str
Effort level for reasoning models: "low", "medium", or "high".
thinking_tokens
int
Maximum extended-thinking budget for Claude reasoning models.
safety_prompt_on
bool
default:"false"
Prepend the framework’s safety preamble to the system prompt.
random_models_on
bool
default:"false"
Randomly select from a pool of models on each call (load-balancing/experimentation).
tokenizer
Any
default:"None"
Optional tokenizer instance used for local token counting.
workspace_dir
str
The workspace directory for the agent. Controlled by the WORKSPACE_DIR environment variable (defaults to agent_workspace). Each agent gets its own subdirectory at workspace_dir/agents/{agent-name}-{uuid}/.
load_state_path
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.
task
Union[str, Any]
The task or prompt for the agent to process
img
str
Optional image path or data for vision-enabled models
imgs
List[str]
Optional list of image paths for batch processing
correct_answer
str
Expected correct answer for validation with automatic retries
streaming_callback
Callable[[str], None]
Callback function to receive streaming tokens in real-time
return
Any
Agent output formatted according to output_type configuration
Return types based on input: Examples:

call

Alternative syntax for running the agent (calls run internally).

arun

Async version of run.

run_batched

Run multiple tasks concurrently in batch mode.
tasks
List[str]
List of tasks to run concurrently
imgs
List[str]
List of images to process with each task
return
List[Any]
List of results from each task execution, in the same order as the input tasks

run_multiple_images

Run the agent with multiple images using concurrent processing.

continuous_run_with_answer

Run the agent until the correct answer is provided.

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.
Tool-call results are fed back into the loop automatically — tokens from each subsequent LLM turn (synthesis turn, autonomous summary phase, etc.) are streamed through as well.

arun_stream

Async generator version of run_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

Run a single task concurrently using the agent’s internal executor. Returns the awaited result.

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 to run.

save

Save the agent’s current state to disk.

load

Load agent state from a saved file.

save_state

Save the current state of the agent to a JSON file.

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.

memory_query

Query the long-term memory for relevant information.

ingest_docs / ingest_pdf

Ingest documents into the agent’s memory.

talk_to

Initiate a conversation with another agent.

talk_to_multiple_agents

Talk to multiple agents concurrently.

receive_message / send_agent_message

Receive or send messages between agents.

handle_handoffs

Handle task delegation to specialized agents when handoffs are configured.

reset

Reset the agent’s memory and state.

undo_last

Undo the last response and return the previous state.

plan

Run only the planning phase for a task without executing.
Display the agent’s configuration dashboard.

showcase_config

Display the agent’s configuration in a formatted table.

analyze_feedback

Analyze the feedback for issues.

update_system_prompt / update_max_loops / update_loop_interval / update_retry_attempts / update_retry_interval

In-place setters for runtime reconfiguration.

enable_autosave / disable_autosave / cleanup

Control the agent’s background autosave loop.

get_llm_parameters

Returns the parameters of the language model.

get_agent_role

Returns the role of the agent.

Complete Methods Reference

Advanced Capabilities

Tool Integration

The Agent 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.
You can also provide tool schemas in OpenAI function-calling dictionary format via tools_list_dictionary:

Long-term Memory Management

The agent supports integration with vector databases for long-term memory management:

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

By default (persistent_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. Set persistent_memory=False to disable all on-disk state — the agent starts from a blank slate every run.

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

The Agent 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:
  1. Task Analysis: When a task is received, the main agent uses a built-in “boss agent” to analyze the task requirements
  2. Agent Selection: The boss agent evaluates all available specialized agents and selects the most suitable one(s)
  3. Task Delegation: The selected agent(s) receive the task and process it
  4. Response Aggregation: Results from specialized agents are collected and returned

Autonomous Mode

When max_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

When max_loops="auto" and interactive=False, the agent has access to specialized tools: All file operations use the agent’s workspace directory ($WORKSPACE_DIR/agents/{agent-name}-{uuid}/).

Sub-Agent Delegation

The autonomous agent can create and manage sub-agents for parallel task execution:

Batch Processing

Process multiple tasks efficiently with run_batched:

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

When autosave=True, the agent saves its configuration at each loop step to workspace_dir/agents/{agent-name}-{uuid}/config.json. Files are written atomically to prevent corruption.
The workspace directory is controlled by the WORKSPACE_DIR environment variable (defaults to agent_workspace).

Async and Concurrent Execution

Comprehensive Agent Configuration

Various Settings

Output Types

The agent supports multiple output formats via the output_type parameter:
  • "str" or "string": Returns the last response as a string
  • "str-all-except-first": Returns all responses except system prompt as string (default)
  • "list": Returns conversation as a list of messages
  • "json": Returns conversation as JSON string
  • "dict": Returns conversation as dictionary
  • "yaml": Returns conversation as YAML string
  • "xml": Returns conversation as XML string

Error Handling

The Agent class includes comprehensive error handling:
  • AgentInitializationError: Raised when agent fails to initialize
  • AgentRunError: Raised when execution fails
  • AgentLLMError: Raised when LLM encounters issues
  • AgentToolError: Raised when tool execution fails
  • AgentMemoryError: Raised for memory-related issues

New Features and Parameters

Enhanced Run Method Parameters

  • imgs: Process multiple images simultaneously instead of just one
  • correct_answer: Validate responses against expected answers with automatic retries
  • streaming_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

  • BaseSwarm - Base class for multi-agent systems
  • BaseStructure - Foundation for swarm structures
  • Tools - Creating and using agent tools
  • Memory - Long-term memory systems