Understanding Agents
Agents are the core building blocks of Legion. Learn how to create and customize agents for your needs.
What is an Agent?
An agent is an autonomous entity that can:
- Process incoming messages
- Make decisions
- Use tools to perform actions
- Communicate with other agents
Creating Agents
Legion provides a decorator-based approach to create agents:
from legion import agent, tool @agent( model="openai:gpt-4o-mini", # Specify the model provider and model temperature=0.2, # Control randomness in responses tools=[add_numbers, multiply] # Bind external tools ) class MathHelper: """An agent that helps with basic arithmetic and string operations. I can perform calculations and manipulate text based on your requests. You have both external tools for math operations and internal tools for formatting. """ # Agent-specific tools can be defined within the agent class @tool def format_result(self, number: float, prefix: str = "Result: ") -> str: """Format a number with a custom prefix.""" return f"{prefix}{number:.2f}"
Agent Properties
model
: The underlying model for decision making (e.g., "openai:gpt-4o-mini", "anthropic:claude-3-opus")temperature
: Creativity vs determinism balance (0.0-1.0)tools
: External tools the agent can usesystem_prompt
: Custom system prompt (can be dynamic)
Supported Model Providers
Legion supports multiple model providers:
- OpenAI (e.g., "openai:gpt-4o", "openai:gpt-4o-mini")
- Anthropic (e.g., "anthropic:claude-3-opus", "anthropic:claude-3-sonnet")
- Gemini (e.g., "gemini:gemini-pro", "gemini:gemini-ultra")
- Groq (e.g., "groq:llama3-70b-8192", "groq:mixtral-8x7b-32768")
- Ollama (e.g., "ollama:llama3", "ollama:mistral")
Using Agents
Once you've created an agent, you can use it to process messages:
# Create an instance of the agent agent = MathHelper() # Process a message synchronously response = agent.process("Can you add 2 and 3?") print(response.content) # Process a message asynchronously response = await agent.aprocess("Can you multiply 4 by 5?") print(response.content)
Next Steps
Learn more about: