π Build an LLM Agent in 5 minutes using OpenRouter!
A lightweight, easy-to-understand LLM Agent framework that works with any model on OpenRouter - including Claude, GPT-4, Llama, Gemini, and more.
- π Universal Compatibility: Works with 100+ models through OpenRouter's unified API
- π‘ Simple to Learn: Clear, readable code that teaches agent fundamentals
- π§ Easy Tool Integration: Add custom tools with simple Python decorators
- β‘ Lightweight: Minimal dependencies, easy to modify and extend
- π€ ReAct Pattern: Uses the proven Reason-Act-Observe loop for reliable tool usage
OpenRouter provides a unified API to access many AI models:
- Anthropic Claude (3.5 Sonnet, 3 Opus, etc.)
- OpenAI GPT (GPT-4, GPT-3.5-turbo)
- Google Gemini
- Meta Llama
- And 100+ more models!
You only need one API key to access all of them.
# Clone or download this repository
cd MiniAgent-OpenRouter
# Install dependencies
pip install -r requirements.txt- Go to OpenRouter
- Sign up for an account
- Get your API key from the dashboard
- Add credits to your account (starts at $1)
Create a .env file from the example:
cp .env.example .envEdit .env and add your API key:
OPENROUTER_API_KEY=your_api_key_here
OPENROUTER_MODEL=anthropic/claude-3.5-sonnet# Simple example with built-in tools
python examples/simple_example.py
# Custom tools example
python examples/custom_tools_example.pyfrom miniagent import MiniAgent
# Initialize agent with your preferred model
agent = MiniAgent(
model="anthropic/claude-3.5-sonnet", # or any OpenRouter model
api_key="your_openrouter_api_key"
)
# Load built-in tools
agent.load_builtin_tool("calculator")
agent.load_builtin_tool("get_current_time")
# Ask a question
response = agent.run("What time is it? Also calculate 15 * 23.")
print(response)from miniagent import MiniAgent, register_tool
# Define a custom tool
@register_tool
def get_weather(city: str) -> str:
"""
Get weather information for a city
Args:
city: Name of the city
"""
# Your weather API call here
return f"Weather in {city}: Sunny, 72Β°F"
# Create agent
agent = MiniAgent(
model="anthropic/claude-3.5-sonnet",
api_key="your_key"
)
# Add your custom tool
from miniagent import get_tool_description
agent.tools.append(get_tool_description(get_weather))
# Use it!
response = agent.run("What's the weather in San Francisco?")Here are some popular models you can use:
anthropic/claude-3.5-sonnet(Recommended - best balance)anthropic/claude-3-opus(Most capable)anthropic/claude-3-sonnetanthropic/claude-3-haiku(Fastest, cheapest)
openai/gpt-4-turboopenai/gpt-4openai/gpt-3.5-turbo
google/gemini-progoogle/gemini-pro-vision
meta-llama/llama-3-70b-instructmeta-llama/llama-3-8b-instruct
See the full list at OpenRouter Models
MiniAgent comes with these built-in tools:
- calculator: Safe mathematical expression evaluation
- get_current_time: Get current date and time
- system_info: Get system information
MiniAgent uses the ReAct (Reason + Act) pattern:
- Reason: The LLM thinks about what to do
- Act: It decides to use a tool
- Observe: It sees the tool's result
- Repeat: Until it has enough info to answer
Example flow:
User: "What is 25 * 4 and what time is it?"
LLM Thinks: "I need to calculate 25 * 4 and get the time"
LLM Acts: Uses calculator tool with "25 * 4"
Tool Returns: 100
LLM Thinks: "Good, now I need the time"
LLM Acts: Uses get_current_time tool
Tool Returns: "2025-11-12 14:30:00"
LLM Thinks: "I have all the information now"
LLM Answers: "25 * 4 = 100, and the current time is 2:30 PM"
agent = MiniAgent(
model="anthropic/claude-3.5-sonnet",
api_key="your_key",
temperature=0.7, # Creativity level (0.0-1.0)
max_tokens=4096, # Max response length
max_iterations=10, # Max reasoning loops
system_prompt="custom..." # Custom instructions
)MiniAgent-OpenRouter/
βββ miniagent/
β βββ __init__.py # Main exports
β βββ agent.py # Core agent logic
β βββ logger.py # Logging utilities
β βββ tools/
β βββ __init__.py # Tool registration
β βββ basic_tools.py # Built-in tools
βββ examples/
β βββ simple_example.py # Basic usage
β βββ custom_tools_example.py # Custom tools
βββ .env.example # Configuration template
βββ requirements.txt # Dependencies
βββ README.md # This file
OpenRouter charges based on the model you use. Tips:
-
Start with cheaper models for testing:
anthropic/claude-3-haikuopenai/gpt-3.5-turbo
-
Use rate limits in your code
-
Monitor usage in OpenRouter dashboard
-
Set a
max_iterationslimit to prevent runaway costs
- Check your API key in
.env - Ensure you have credits on OpenRouter
- Verify model name at OpenRouter Models
- Check for typos in model identifier
- Make sure tools are loaded before calling
run() - Check if the model supports function calling (most do)
- Try adjusting the
system_prompt
- Increase
max_iterationsparameter - Simplify your query
- Try a more capable model
This is a learning-focused project. Contributions welcome:
- Fork the repository
- Create a feature branch
- Make your changes
- Submit a pull request
Apache License 2.0 - see LICENSE file
Based on the original MiniAgent by ZhuLinsen, adapted for OpenRouter compatibility.
- OpenRouter Docs: https://openrouter.ai/docs
- Issues: GitHub Issues
- Questions: Open a discussion
Happy Agent Building! π€β¨