Basic Agent Example
Learn how to create and run your first autonomous agent using the Swarms framework. An Agent is the fundamental building block of a swarm—an autonomous entity powered by an LLM + Tools + Memory.Quick Start
Here’s the simplest way to create and run an agent:Understanding the Code
Let’s break down each component:1. Import the Agent Class
Agent class is the core building block for creating autonomous agents in Swarms.
2. Initialize the Agent
model_name: The LLM to power your agent. Swarms supports OpenAI, Anthropic, Groq, Ollama, and more.max_loops: Controls iteration behavior:"auto": Agent decides when to stop based on task completion- Integer (e.g.,
1,5): Fixed number of iterations
interactive: WhenTrue, provides real-time feedback during execution
3. Run the Agent
run() method executes the agent with your task and returns the response.
Example Output
When you run this agent, you’ll see output similar to:Customizing Your Agent
Here’s a more customized example with additional configuration:Additional Configuration Options
agent_name: A unique identifier for your agentagent_description: Describes the agent’s purpose and capabilitiessystem_prompt: Instructions that define the agent’s behavior and personalityverbose: WhenTrue, shows detailed execution logsoutput_type: Controls the format of the response ("str","json","dict", etc.)
Working with Different Models
Swarms supports multiple LLM providers:Environment Setup
Make sure you have the required API keys set in your environment:.env file:
Next Steps
Now that you’ve created your first agent, explore these advanced topics:- Agent with Tools - Enhance agents with external tools
- Vision Agent - Process images and multimodal content
- Streaming Responses - Stream agent outputs in real-time
- Multi-Agent Workflows - Coordinate multiple agents
Common Patterns
Task-Specific Agent
Autonomous Agent with Auto Loops
Tips and Best Practices
- Start Simple: Begin with basic configurations and add complexity as needed
- Use Descriptive Names: Clear agent names and descriptions improve debugging
- Set Appropriate Loop Limits: Use
max_loops=1for simple tasks, higher values or"auto"for complex ones - Monitor Costs: Be mindful of API costs when using
max_loops="auto" - Test Incrementally: Test your agent with simple tasks before moving to complex ones