Overview
TheAgent 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
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
Unique identifier for the agent instance
The name of the agent, used for identification and logging
A description of the agent’s purpose and capabilities
The system prompt that defines the agent’s behavior and personality
The language model instance to use. If None, a LiteLLM instance will be created
The name of the model to use (e.g., “gpt-4o”, “claude-3-opus”)
Maximum number of reasoning loops. Use “auto” for autonomous mode with dynamic planning
List of callable functions that the agent can use as tools
Temperature for LLM sampling (0.0 to 1.0)
Maximum number of tokens in the LLM response
Context window size. Automatically set based on model_name if not specified
Enable basic streaming with formatted panels
Enable detailed token-by-token streaming with metadata (citations, tokens used, etc.)
Callback function to receive streaming tokens in real-time
Enable interactive mode for user input between loops
Enable verbose logging for debugging
Return the full conversation history instead of just the final response
Output format: ‘str’, ‘string’, ‘list’, ‘json’, ‘dict’, ‘yaml’, ‘xml’
Automatically save agent state during execution
Display agent dashboard on initialization
Long-term memory system (e.g., vector database) for RAG
Query RAG memory on every loop iteration
List of model names to try in order. First model is primary, rest are fallbacks
Number of retry attempts for LLM calls
Interval in seconds between retry attempts
Token that signals the agent to stop execution
Function that returns True when the agent should stop
Alternative stopping function
Enable dynamic temperature adjustment during execution
Enable dynamic loop count adjustment (sets max_loops=“auto”)
Name of the user in conversation history
Path to save agent state
Standard operating procedure for the agent
List of standard operating procedures
Rules that govern agent behavior
Prompt for planning phase
Enable planning phase before execution
Enable multi-modal processing (images, etc.)
Timeout for agent execution in seconds
Enable artifact generation and storage
Path to save artifacts
File extension for artifacts (.pdf, .md, .txt)
URL or connection object for a single MCP server
List of MCP server URLs for multiple connections
MCP connection configuration object
Configuration for multiple MCP connections
List of agents to enable task handoffs/delegation
List of agent capabilities for documentation
Execution mode: interactive (with user input), fast (minimal logging), or standard
UUID of a prompt from the Swarms marketplace to use as system prompt
Path to directory containing Agent Skills in SKILL.md format (Anthropic framework)
Tools to enable for autonomous looper when max_loops=“auto”. Options: “all” or list of tool names
Enable reasoning mode for supported models (e.g., o1, o3)
Effort level for reasoning models (“low”, “medium”, “high”)
Maximum thinking tokens for reasoning models
Methods
run
Execute the agent’s main loop for a given task.The task or prompt for the agent to process
Optional image path or data for vision-enabled models
Agent output formatted according to output_type configuration
call
Alternative syntax for running the agent (callsrun internally).
run_concurrent
Run multiple tasks concurrently.bulk_run
Execute multiple tasks in batch.save
Save the agent’s current state to disk.load
Load agent state from a saved file.to_dict
Convert agent configuration to dictionary.to_json
Convert agent configuration to JSON string.to_yaml
Convert agent configuration to YAML string.add_tool
Dynamically add a tool to the agent.reset
Reset the agent’s memory and state.print_dashboard
Display the agent’s configuration dashboard.Examples
Basic Usage
Agent with Tools
Multi-modal Agent
Autonomous Agent with Auto Loops
Agent with Streaming
Agent with Fallback Models
Agent with MCP Integration
Agent Handoffs
Marketplace Prompt Loading
Output Types
The agent supports multiple output formats via theoutput_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
- Set appropriate max_loops: Use 1 for simple tasks, higher numbers for complex reasoning, or “auto” for autonomous planning
- Use tools wisely: Provide well-documented tools with clear function signatures and docstrings
- Enable autosave for long-running tasks: Prevents data loss on interruption
- Configure fallback models: Ensures reliability in production
- Use streaming for real-time feedback: Better user experience for long-running tasks
- Set context_length appropriately: Prevents token limit errors
- Enable verbose mode during development: Helps debug issues
- Use agent handoffs for complex workflows: Delegate subtasks to specialized agents
Related
- BaseSwarm - Base class for multi-agent systems
- BaseStructure - Foundation for swarm structures
- Tools - Creating and using agent tools
- Memory - Long-term memory systems