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.
The Agent class provides extensive configuration options to customize behavior, performance, and capabilities.
Core Parameters
Model Configuration
The name of the language model to use. Supports any model from OpenAI, Anthropic, Groq, Cohere, and more via LiteLLM. agent = Agent( model_name = "claude-sonnet-4-6" )
agent = Agent( model_name = "claude-sonnet-4-5" )
agent = Agent( model_name = "groq/llama-3.1-70b" )
Pre-configured LLM instance. If not provided, will be created automatically based on model_name. from swarms.utils.litellm_wrapper import LiteLLM
llm = LiteLLM( model_name = "claude-sonnet-4-6" , temperature = 0.5 )
agent = Agent( llm = llm)
Controls randomness in model outputs (0.0 = deterministic, 1.0 = creative). # Deterministic for code generation
agent = Agent( model_name = "claude-sonnet-4-6" , temperature = 0.1 )
# Creative for writing
agent = Agent( model_name = "claude-sonnet-4-6" , temperature = 0.9 )
Maximum number of tokens to generate in a single response. agent = Agent( model_name = "claude-sonnet-4-6" , max_tokens = 8192 )
context_length
int
default: "Based on model"
Maximum context window size. Automatically set based on the model. agent = Agent( model_name = "claude-sonnet-4-6" , context_length = 128000 )
Agent Identity
agent_name
str
default: "swarm-worker-01"
Unique name for the agent. Used in multi-agent systems and logging. agent = Agent(
agent_name = "Financial-Analyst" ,
model_name = "claude-sonnet-4-6"
)
agent_description
str
default: "Auto-generated"
Description of the agent’s purpose and capabilities. agent = Agent(
agent_name = "Data-Analyst" ,
agent_description = "Expert in data analysis, visualization, and statistical modeling" ,
model_name = "claude-sonnet-4-6"
)
system_prompt
str
default: "Default system prompt"
The system prompt that defines agent behavior and expertise. SYSTEM_PROMPT = """
You are a senior software engineer specializing in:
- Clean code architecture
- Test-driven development
- Code review best practices
Provide detailed, well-documented code solutions.
"""
agent = Agent(
system_prompt = SYSTEM_PROMPT ,
model_name = "claude-sonnet-4-6"
)
Execution Control
max_loops
Union[int, str]
default: "1"
Number of execution loops. Set to “auto” for autonomous mode. # Single execution
agent = Agent( max_loops = 1 )
# Multi-step reasoning
agent = Agent( max_loops = 5 )
# Autonomous mode
agent = Agent( max_loops = "auto" )
Delay in seconds between loops. agent = Agent( max_loops = 5 , loop_interval = 1 ) # 1 second delay
Number of retry attempts for failed LLM calls. agent = Agent( retry_attempts = 5 )
Delay in seconds between retry attempts. agent = Agent( retry_attempts = 3 , retry_interval = 2 )
Timeout in seconds for agent execution. agent = Agent( timeout = 300 ) # 5 minute timeout
Output Configuration
output_type
str
default: "str-all-except-first"
Format for agent output. Options: “str”, “list”, “json”, “dict”, “yaml”, “xml”. # String output
agent = Agent( output_type = "str" )
# JSON output
agent = Agent( output_type = "json" )
# Dictionary output
agent = Agent( output_type = "dict" )
Enable basic streaming with formatted panels. agent = Agent( streaming_on = True )
Enable detailed token-by-token streaming with metadata. agent = Agent( stream = True )
response = agent.run( "Tell me a story" ) # Streams each token
Callback function to receive streaming tokens in real-time. def on_token ( token : str ):
print ( f "Token: { token } " , end = "" , flush = True )
agent = Agent( streaming_callback = on_token)
Streaming Methods
In addition to the streaming flags above, the Agent exposes two streaming methods that yield tokens as a generator. They are real LLM streaming — tokens are forwarded the moment LiteLLM emits them, across every loop of the agent (tool-call turns, synthesis turns, autonomous plan/execute/summary phases).
agent.run_stream(task) -> Iterator[str]
Sync generator that yields tokens. The agent runs in a background thread; tokens are pushed onto a queue and yielded to the caller in order.
from swarms import Agent
agent = Agent(
agent_name = "Writer" ,
model_name = "gpt-4.1-mini" ,
max_loops = 3 ,
)
for token in agent.run_stream( "Write a short poem about distributed systems." ):
print (token, end = "" , flush = True )
agent.arun_stream(task) -> AsyncIterator[str]
Async generator. Same semantics as run_stream, but 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.
import asyncio
from swarms import Agent
agent = Agent(
agent_name = "Writer" ,
model_name = "gpt-4.1-mini" ,
max_loops = 1 ,
)
async def main ():
async for token in agent.arun_stream( "Explain async/await in two sentences." ):
print (token, end = "" , flush = True )
asyncio.run(main())
Both methods stream tokens through every internal loop, including tool calls, synthesis turns after a tool returns, and the autonomous plan/execute/summary cycle when max_loops="auto".
Enable detailed logging output. agent = Agent( verbose = True )
Enable printing of agent responses. agent = Agent( print_on = True )
Memory and History
Return full conversation history instead of just final response. agent = Agent( return_history = True )
response = agent.run( "Hello" ) # Returns full conversation
Name to use for user messages in conversation history. agent = Agent( user_name = "John" )
Automatically manage context window to prevent overflow. agent = Agent( dynamic_context_window = True )
Advanced Features
dynamic_temperature_enabled
Randomly adjust temperature between loops for varied outputs. agent = Agent( dynamic_temperature_enabled = True )
Add reasoning prompts to guide multi-step thinking. agent = Agent( max_loops = 5 , reasoning_prompt_on = True )
Enable interactive mode for conversational agents. agent = Agent( interactive = True )
Display agent dashboard on initialization. agent = Agent( dashboard = True )
State Management
Automatically save agent state after each execution. agent = Agent( autosave = True )
saved_state_path
str
default: "Auto-generated"
Path to save agent state. agent = Agent(
autosave = True ,
saved_state_path = "./states/my_agent.json"
)
Path to load previous agent state from. agent = Agent( load_state_path = "./states/my_agent.json" )
Reliability
List of fallback models to try if primary model fails. agent = Agent(
fallback_models = [
"claude-sonnet-4-6" ,
"gpt-5.4" ,
"gpt-3.5-turbo"
]
)
Agent execution mode. Options: “interactive”, “fast”, “standard”. # Fast mode (no printing, minimal overhead)
agent = Agent( mode = "fast" )
# Interactive mode
agent = Agent( mode = "interactive" )
Nucleus sampling parameter for model generation. agent = Agent( top_p = 0.95 )
Example Configurations
Production Agent
production_agent = Agent(
agent_name = "Production-Agent" ,
agent_description = "Production-ready agent with reliability features" ,
model_name = "claude-sonnet-4-6" ,
fallback_models = [ "claude-sonnet-4-6" , "gpt-5.4" ],
max_loops = 1 ,
temperature = 0.3 ,
max_tokens = 4096 ,
retry_attempts = 5 ,
retry_interval = 2 ,
timeout = 300 ,
autosave = True ,
verbose = True ,
dynamic_context_window = True ,
)
Research Agent
research_agent = Agent(
agent_name = "Research-Agent" ,
system_prompt = "Expert researcher providing detailed analysis" ,
model_name = "claude-sonnet-4-6" ,
max_loops = 3 ,
temperature = 0.5 ,
reasoning_prompt_on = True ,
verbose = True ,
return_history = True ,
)
Fast Batch Processing Agent
batch_agent = Agent(
agent_name = "Batch-Processor" ,
model_name = "gpt-5.4" ,
max_loops = 1 ,
mode = "fast" , # Disable printing for performance
temperature = 0.2 ,
print_on = False ,
verbose = False ,
)
Next Steps
Agent Memory Configure conversation history and memory
Agent Tools Add tools to extend capabilities
Reference
Location in source: swarms/structs/agent.py:352-454