> ## Documentation Index
> Fetch the complete documentation index at: https://docs.swarms.world/llms.txt
> Use this file to discover all available pages before exploring further.

# Agent

> The core Agent class for building autonomous AI agents with tools, memory, and multi-modal capabilities

## Overview

The `Agent` class is the backbone 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.

## Import

```python theme={null}
from swarms import Agent
```

## 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

<ParamField path="id" type="str" default="agent-{uuid}">
  Unique identifier for the agent instance
</ParamField>

<ParamField path="agent_name" type="str" default="swarm-worker-01">
  The name of the agent, used for identification and logging
</ParamField>

<ParamField path="agent_description" type="str" default="An autonomous agent that can perform tasks and learn from experience powered by Swarms">
  A description of the agent's purpose and capabilities. Shown to orchestrators when routing tasks.
</ParamField>

<ParamField path="system_prompt" type="str">
  The system prompt that defines the agent's behavior and personality
</ParamField>

<ParamField path="llm" type="Any">
  The language model instance to use. If None, a LiteLLM instance will be created
</ParamField>

<ParamField path="model_name" type="str" default="gpt-4.1">
  The LiteLLM-compatible model identifier (e.g. `"gpt-4.1"`, `"claude-sonnet-4-6"`, `"groq/llama-3.3-70b-versatile"`).
</ParamField>

<ParamField path="llm_args" type="dict" default="None">
  Extra keyword arguments forwarded to the underlying LiteLLM client.
</ParamField>

<ParamField path="llm_base_url" type="str" default="None">
  Base URL for OpenAI-compatible providers (Ollama, LM Studio, vLLM, etc.).
</ParamField>

<ParamField path="llm_api_key" type="str" default="None">
  Override API key for the LLM provider. Falls back to environment variables when unset.
</ParamField>

<ParamField path="fallback_model_name" type="str" default="None">
  Single fallback model used when the primary model fails.
</ParamField>

<ParamField path="max_loops" type="Union[int, str]" default="1">
  Maximum number of reasoning loops. Use "auto" for autonomous mode with dynamic planning
</ParamField>

<ParamField path="tools" type="List[Callable]">
  List of callable functions that the agent can use as tools
</ParamField>

<ParamField path="temperature" type="float" default="0.5">
  Temperature for LLM sampling (0.0 to 1.0)
</ParamField>

<ParamField path="max_tokens" type="int" default="4096">
  Maximum number of tokens in the LLM response
</ParamField>

<ParamField path="context_length" type="int" default="16000">
  Effective context window in tokens. When `context_compression=True`, the agent compresses memory once usage crosses 90% of this limit.
</ParamField>

<ParamField path="top_p" type="float" default="None">
  Nucleus-sampling parameter. Stripped automatically for Anthropic models when extended thinking is enabled.
</ParamField>

<ParamField path="dynamic_context_window" type="bool" default="True">
  Allow the framework to grow/shrink the per-call context budget based on token usage signals.
</ParamField>

<ParamField path="context_compression" type="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.
</ParamField>

<ParamField path="persistent_memory" type="bool" default="True">
  When `True`, read/write `MEMORY.md` under the workspace so agent state survives process restarts. Set `False` for stateless tasks.
</ParamField>

<ParamField path="transforms" type="Union[TransformConfig, dict]" default="None">
  Optional pre/post-processing transforms applied to the conversation history.
</ParamField>

<ParamField path="streaming_on" type="bool" default="false">
  Enable basic streaming with formatted panels
</ParamField>

<ParamField path="stream" type="bool" default="false">
  Enable detailed token-by-token streaming with metadata (citations, tokens used, etc.)
</ParamField>

<ParamField path="streaming_callback" type="Callable[[str], None]">
  Callback function to receive streaming tokens in real-time. Use with `agent.run_stream` / `agent.arun_stream` for generator-style consumption.
</ParamField>

<ParamField path="interactive" type="bool" default="false">
  Enable interactive mode (REPL-style) — prompt the user for input between loops.
</ParamField>

<ParamField path="verbose" type="bool" default="false">
  Enable verbose logging for debugging.
</ParamField>

<ParamField path="print_on" type="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.
</ParamField>

<ParamField path="output_type" type="OutputType" default="str-all-except-first">
  Output format: 'str', 'string', 'list', 'json', 'dict', 'yaml', 'xml'
</ParamField>

<ParamField path="autosave" type="bool" default="false">
  Automatically save agent state during execution
</ParamField>

<ParamField path="dashboard" type="bool" default="false">
  Display agent dashboard on initialization
</ParamField>

<ParamField path="long_term_memory" type="Union[Callable, Any]">
  Long-term memory backend (e.g. vector database) for RAG.
</ParamField>

<ParamField path="fallback_models" type="List[str]">
  List of fallback models to try in order if the primary model fails.
</ParamField>

<ParamField path="retry_attempts" type="int" default="3">
  Number of retry attempts for LLM calls
</ParamField>

<ParamField path="retry_interval" type="int" default="1">
  Interval in seconds between retry attempts
</ParamField>

<ParamField path="stopping_token" type="str">
  Token that signals the agent to stop execution
</ParamField>

<ParamField path="stopping_condition" type="Callable[[str], bool]">
  Function that returns True when the agent should stop
</ParamField>

<ParamField path="stopping_func" type="Callable">
  Alternative stopping function
</ParamField>

<ParamField path="dynamic_temperature_enabled" type="bool" default="false">
  Enable dynamic temperature adjustment during execution
</ParamField>

<ParamField path="dynamic_loops" type="bool" default="false">
  Enable dynamic loop count adjustment (sets max\_loops="auto")
</ParamField>

<ParamField path="loop_interval" type="int" default="0">
  Seconds to wait between consecutive loop iterations.
</ParamField>

<ParamField path="custom_exit_command" type="str" default="exit">
  Token the user can type in interactive mode to exit the loop.
</ParamField>

<ParamField path="preset_stopping_token" type="bool" default="false">
  When `True`, append the framework's preset stopping marker to the system prompt.
</ParamField>

<ParamField path="auto_generate_prompt" type="bool" default="false">
  Auto-generate a system prompt from the task description when one is not provided.
</ParamField>

<ParamField path="user_name" type="str" default="Human">
  Name of the user in conversation history
</ParamField>

<ParamField path="saved_state_path" type="str">
  Path to save agent state
</ParamField>

<ParamField path="sop" type="str">
  Standard operating procedure for the agent
</ParamField>

<ParamField path="sop_list" type="List[str]">
  List of standard operating procedures
</ParamField>

<ParamField path="rules" type="str">
  Rules that govern agent behavior
</ParamField>

<ParamField path="planning_prompt" type="str">
  Prompt for planning phase
</ParamField>

<ParamField path="plan_enabled" type="bool" default="false">
  Enable planning phase before execution
</ParamField>

<ParamField path="multi_modal" type="bool">
  Enable multi-modal processing (images, etc.).
</ParamField>

<ParamField path="summarize_multiple_images" type="bool" default="false">
  When multiple images are provided, summarise them into a single context entry before invoking the LLM.
</ParamField>

<ParamField path="tool_call_summary" type="bool" default="True">
  After every tool call, run a brief LLM summary of the tool result and add it to the conversation.
</ParamField>

<ParamField path="tool_retry_attempts" type="int" default="3">
  Number of times to retry a failing tool call before giving up.
</ParamField>

<ParamField path="show_tool_execution_output" type="bool" default="True">
  Display tool inputs/outputs in the agent's printed output.
</ParamField>

<ParamField path="tools_list_dictionary" type="List[Dict[str, Any]]" default="None">
  Pre-built OpenAI function-calling tool schemas. Use when you want to bypass the auto-generated schema.
</ParamField>

<ParamField path="tool_schema" type="ToolUsageType" default="None">
  Override tool schema used at runtime.
</ParamField>

<ParamField path="output_cleaner" type="Callable" default="None">
  Optional post-processor applied to the agent's output before returning.
</ParamField>

<ParamField path="list_base_models" type="List[BaseModel]" default="None">
  Pydantic models registered for structured-output prompting.
</ParamField>

<ParamField path="mcp_url" type="Union[str, MCPConnection]">
  URL or connection object for a single MCP server.
</ParamField>

<ParamField path="mcp_urls" type="List[str]">
  List of MCP server URLs for connecting to multiple servers.
</ParamField>

<ParamField path="mcp_config" type="MCPConnection">
  Single MCP connection configuration object.
</ParamField>

<ParamField path="handoffs" type="Union[Sequence[Callable], Any]">
  List of agents to enable task handoffs/delegation
</ParamField>

<ParamField path="capabilities" type="List[str]">
  Free-form list of agent capabilities used for routing and documentation.
</ParamField>

<ParamField path="role" type="agent_roles" default="worker">
  The agent's role within a swarm (e.g. `"worker"`, `"director"`).
</ParamField>

<ParamField path="tags" type="List[str]" default="None">
  Tags used to filter or categorise the agent.
</ParamField>

<ParamField path="use_cases" type="List[Dict[str, Any]]" default="None">
  Structured list of intended use cases for documentation/marketplace listings.
</ParamField>

<ParamField path="mode" type="Literal['interactive', 'fast', 'standard']" default="standard">
  Execution mode: `interactive` (REPL), `fast` (minimal logging/decoration), or `standard`.
</ParamField>

<ParamField path="marketplace_prompt_id" type="str">
  UUID of a prompt from the Swarms marketplace to use as the system prompt.
</ParamField>

<ParamField path="publish_to_marketplace" type="bool" default="false">
  When `True`, publish this agent to the Swarms marketplace on initialization.
</ParamField>

<ParamField path="skills_dir" type="str">
  Path to a directory of Agent Skills (Anthropic `SKILL.md` format).
</ParamField>

<ParamField path="selected_tools" type="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.
</ParamField>

<ParamField path="react_on" type="bool" default="false">
  Enable ReAct-style reasoning prompting.
</ParamField>

<ParamField path="reasoning_prompt_on" type="bool" default="True">
  Whether to prepend the framework's reasoning preamble to the system prompt.
</ParamField>

<ParamField path="reasoning_enabled" type="bool" default="false">
  Enable reasoning mode for supported models (e.g. o1, o3, Claude with extended thinking).
</ParamField>

<ParamField path="reasoning_effort" type="str">
  Effort level for reasoning models: `"low"`, `"medium"`, or `"high"`.
</ParamField>

<ParamField path="thinking_tokens" type="int">
  Maximum extended-thinking budget for Claude reasoning models.
</ParamField>

<ParamField path="safety_prompt_on" type="bool" default="false">
  Prepend the framework's safety preamble to the system prompt.
</ParamField>

<ParamField path="random_models_on" type="bool" default="false">
  Randomly select from a pool of models on each call (load-balancing/experimentation).
</ParamField>

<ParamField path="tokenizer" type="Any" default="None">
  Optional tokenizer instance used for local token counting.
</ParamField>

<ParamField path="load_state_path" type="str" default="None">
  Path from which to load saved agent state on init.
</ParamField>

## Methods

### run

Execute the agent's main loop for a given task.

```python theme={null}
def run(
    task: Optional[Union[str, Any]] = None,
    img: Optional[str] = None,
    *args,
    **kwargs
) -> Any
```

<ParamField path="task" type="Union[str, Any]">
  The task or prompt for the agent to process
</ParamField>

<ParamField path="img" type="str">
  Optional image path or data for vision-enabled models
</ParamField>

<ResponseField name="return" type="Any">
  Agent output formatted according to output\_type configuration
</ResponseField>

### **call**

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

```python theme={null}
def __call__(
    task: str,
    *args,
    **kwargs
) -> str
```

### arun

Async version of `run`.

```python theme={null}
async def arun(
    task: Optional[Union[str, Any]] = None,
    img: Optional[str] = None,
    *args,
    **kwargs,
) -> Any
```

### run\_concurrent

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

```python theme={null}
async def run_concurrent(
    task: str,
    *args,
    **kwargs,
) -> Any
```

### run\_concurrent\_tasks

Run a batch of tasks concurrently via a thread pool.

```python theme={null}
def run_concurrent_tasks(
    tasks: List[str],
    *args,
    **kwargs,
) -> List[Any]
```

### bulk\_run

Generate responses for multiple input sets. Each input is a dict of kwargs forwarded to `run`.

```python theme={null}
def bulk_run(
    inputs: List[Dict[str, Any]],
) -> List[str]
```

### save

Save the agent's current state to disk.

```python theme={null}
def save(
    file_path: str = None
) -> None
```

### load

Load agent state from a saved file.

```python theme={null}
def load(
    file_path: str
) -> Agent
```

### to\_dict

Convert agent configuration to dictionary.

```python theme={null}
def to_dict() -> Dict[str, Any]
```

### to\_json

Convert agent configuration to JSON string.

```python theme={null}
def to_json(
    indent: int = 4
) -> str
```

### to\_yaml

Convert agent configuration to YAML string.

```python theme={null}
def to_yaml(
    indent: int = 4
) -> str
```

### add\_tool / add\_tools

Dynamically add a tool (or list of tools) to the agent at runtime.

```python theme={null}
def add_tool(tool: Callable) -> None
def add_tools(tools: List[Callable]) -> None
```

### remove\_tool / remove\_tools

Remove a previously-registered tool (or list of tools).

```python theme={null}
def remove_tool(tool: Callable) -> None
def remove_tools(tools: List[Callable]) -> None
```

### reset

Reset the agent's memory and state.

```python theme={null}
def reset() -> None
```

### print\_dashboard

Display the agent's configuration dashboard.

```python theme={null}
def print_dashboard() -> None
```

### update\_system\_prompt / update\_max\_loops / update\_loop\_interval / update\_retry\_attempts / update\_retry\_interval

In-place setters for runtime reconfiguration.

```python theme={null}
def update_system_prompt(system_prompt: str) -> None
def update_max_loops(max_loops: Union[int, str]) -> None
def update_loop_interval(loop_interval: int) -> None
def update_retry_attempts(retry_attempts: int) -> None
def update_retry_interval(retry_interval: int) -> None
```

### plan

Run only the planning phase for a task without executing.

```python theme={null}
def plan(task: str, *args, **kwargs) -> None
```

### add\_memory

Append a message to the agent's short-term memory.

```python theme={null}
def add_memory(message: str) -> None
```

### enable\_autosave / disable\_autosave / cleanup

Control the agent's background autosave loop.

```python theme={null}
def enable_autosave(interval: int = 300) -> None
def disable_autosave() -> None
def cleanup() -> None
```

### 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.

```python theme={null}
def run_stream(
    task: str,
    img: Optional[str] = None,
    **kwargs,
) -> Iterator[str]
```

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.

```python theme={null}
for token in agent.run_stream("Analyse NVDA"):
    print(token, end="", flush=True)
```

### 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.

```python theme={null}
async def arun_stream(
    task: str,
    img: Optional[str] = None,
    **kwargs,
) -> AsyncIterator[str]
```

```python theme={null}
import asyncio

async def main():
    async for token in agent.arun_stream("Analyse NVDA"):
        print(token, end="", flush=True)

asyncio.run(main())
```

<Note>
  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.
</Note>

## Examples

### Basic Usage

```python theme={null}
from swarms import Agent

# Create a simple agent
agent = Agent(
    agent_name="Financial-Analyst",
    model_name="claude-sonnet-4-6",
    max_loops=1,
    system_prompt="You are a financial analyst. Provide detailed, data-driven insights."
)

# Run a task
response = agent.run("Analyze the Q4 2024 revenue trends")
print(response)
```

### Agent with Tools

```python theme={null}
from swarms import Agent

def search_web(query: str) -> str:
    """Search the web for information."""
    # Implementation
    return f"Search results for: {query}"

def calculate(expression: str) -> float:
    """Evaluate a mathematical expression."""
    return eval(expression)

agent = Agent(
    agent_name="Research-Agent",
    model_name="claude-sonnet-4-6",
    max_loops=5,
    tools=[search_web, calculate],
    system_prompt="You are a research assistant with web search and calculation abilities."
)

result = agent.run("Search for the population of Tokyo and calculate its growth rate")
```

### Multi-modal Agent

```python theme={null}
agent = Agent(
    agent_name="Vision-Agent",
    model_name="claude-sonnet-4-6",
    multi_modal=True,
    max_loops=1
)

response = agent.run(
    task="Describe what you see in this image and identify any objects",
    img="path/to/image.jpg"
)
```

### Autonomous Agent with Auto Loops

```python theme={null}
agent = Agent(
    agent_name="Autonomous-Developer",
    model_name="claude-sonnet-4-6",
    max_loops="auto",  # Enables autonomous planning and execution
    system_prompt="You are an autonomous software developer."
)

result = agent.run("Build a REST API for a todo application with authentication")
# Agent will:
# 1. Create a plan with subtasks
# 2. Execute each subtask using available tools
# 3. Generate a comprehensive summary
```

### Agent with Streaming

```python theme={null}
def on_token(token: str):
    print(token, end="", flush=True)

agent = Agent(
    agent_name="Streaming-Agent",
    model_name="claude-sonnet-4-6",
    stream=True,
    streaming_callback=on_token,
    max_loops=1
)

response = agent.run("Write a creative story about space exploration")
```

### Agent with Fallback Models

```python theme={null}
agent = Agent(
    agent_name="Reliable-Agent",
    fallback_models=["claude-sonnet-4-6", "gpt-5.4", "gpt-3.5-turbo"],
    max_loops=1
)

# Will try gpt-4o first, then gpt-4o-mini, then gpt-3.5-turbo if each fails
response = agent.run("Generate a market analysis report")
```

### Agent with MCP Integration

```python theme={null}
agent = Agent(
    agent_name="MCP-Agent",
    model_name="claude-sonnet-4-6",
    mcp_url="npx -y @modelcontextprotocol/server-filesystem /path/to/directory",
    max_loops=3
)

result = agent.run("Read the contents of config.json and summarize the settings")
```

### Agent Handoffs

```python theme={null}
# Create specialized agents
researcher = Agent(
    agent_name="Researcher",
    model_name="claude-sonnet-4-6",
    system_prompt="You are a research specialist."
)

writer = Agent(
    agent_name="Writer",
    model_name="claude-sonnet-4-6",
    system_prompt="You are a technical writer."
)

# Create coordinator with handoffs
coordinator = Agent(
    agent_name="Coordinator",
    model_name="claude-sonnet-4-6",
    max_loops=5,
    handoffs=[researcher, writer],
    system_prompt="You coordinate tasks between research and writing teams."
)

result = coordinator.run("Create a comprehensive report on quantum computing")
# Coordinator can delegate research to researcher and writing to writer
```

### Marketplace Prompt Loading

```python theme={null}
# Load a prompt from Swarms marketplace in one line
agent = Agent(
    model_name="claude-sonnet-4-6",
    marketplace_prompt_id="550e8400-e29b-41d4-a716-446655440000",
    max_loops=1
)

response = agent.run("Execute the marketplace prompt task")
# Agent automatically loads system prompt from marketplace
```

## 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

## Best Practices

1. **Set appropriate max\_loops**: Use 1 for simple tasks, higher numbers for complex reasoning, or "auto" for autonomous planning
2. **Use tools wisely**: Provide well-documented tools with clear function signatures and docstrings
3. **Enable autosave for long-running tasks**: Prevents data loss on interruption
4. **Configure fallback models**: Ensures reliability in production
5. **Use streaming for real-time feedback**: Better user experience for long-running tasks
6. **Set context\_length appropriately**: Prevents token limit errors
7. **Enable verbose mode during development**: Helps debug issues
8. **Use agent handoffs for complex workflows**: Delegate subtasks to specialized agents

## Related

* [BaseSwarm](/api/base-swarm) - Base class for multi-agent systems
* [BaseStructure](/api/base-structure) - Foundation for swarm structures
* [Tools](/concepts/tools) - Creating and using agent tools
* [Memory](/agents/agent-memory) - Long-term memory systems
