-
Notifications
You must be signed in to change notification settings - Fork 100
How To Debug Agent Errors
Quick solutions to common errors when building and running AI agents with ConnectOnion. Find your error, get the fix.
- API Key Errors
- Tool Calling Errors
- Agent Behavior Issues
- Import and Installation Errors
- Performance Issues
Full Error:
Error: No OpenAI API key found. Set OPENAI_API_KEY environment variable.
Cause: Missing or incorrectly set API key
Solutions:
1. Create .env file (Recommended):
# Create .env in your project directory
echo "OPENAI_API_KEY=sk-your-key-here" > .env2. Set environment variable:
# Linux/Mac
export OPENAI_API_KEY=sk-your-key-here
# Windows PowerShell
$env:OPENAI_API_KEY="sk-your-key-here"3. Set in code (NOT recommended for production):
import os
os.environ["OPENAI_API_KEY"] = "sk-your-key-here"Verify it works:
import os
print(os.getenv("OPENAI_API_KEY")) # Should print your keyFull Error:
AuthenticationError: Invalid API key provided
Causes & Solutions:
1. Wrong API key format:
# ✗ Wrong
OPENAI_API_KEY=your-key-here # Missing sk- prefix
# ✓ Correct
OPENAI_API_KEY=sk-proj-abc123... # OpenAI project key
OPENAI_API_KEY=sk-abc123... # OpenAI regular key2. Key from wrong provider:
# If using Anthropic:
ANTHROPIC_API_KEY=sk-ant-... # NOT OPENAI_API_KEY
# If using Google:
GOOGLE_API_KEY=... # NOT OPENAI_API_KEY3. Expired or revoked key:
- Go to OpenAI API Keys
- Create a new key
- Update your
.envfile
Full Error:
RateLimitError: You exceeded your current quota
Solutions:
1. You're out of credits:
- Check OpenAI Usage
- Add payment method and credits
2. Too many requests:
import time
# Add delay between requests
agent.input("Task 1")
time.sleep(1) # Wait 1 second
agent.input("Task 2")3. Use cheaper model:
# Instead of gpt-4
agent = Agent("assistant", model="gpt-4o-mini") # Much cheaperSymptom: Agent responds but never uses your tools
Causes & Solutions:
1. Missing or unclear docstring:
# ✗ Agent doesn't know what this does
def search(q):
return results
# ✓ Clear description
def search(query: str) -> str:
"""
Search the web for information.
Use this when the user needs current information or facts.
Args:
query: What to search for
Returns:
Search results
"""
return results2. Tool name too generic:
# ✗ Unclear what this does
def process(data):
pass
# ✓ Descriptive name
def search_customer_database(customer_name: str):
pass3. System prompt conflicts:
# ✗ Discourages tool use
system_prompt="Answer all questions directly without using any tools"
# ✓ Encourages tool use
system_prompt="Use the search tool when you need current information"4. Missing type hints:
# ✗ Agent doesn't know parameter types
def calculate(a, b):
return a + b
# ✓ Clear types
def calculate(a: float, b: float) -> float:
return a + bSymptom: Agent uses tool A when it should use tool B
Solutions:
1. Improve docstrings to clarify when to use each tool:
def search_web(query: str) -> str:
"""
Search the INTERNET for current information, news, facts.
Use this for:
- Current events
- General knowledge
- External information
Do NOT use for:
- Internal company data (use search_database instead)
"""
pass
def search_database(query: str) -> str:
"""
Search the INTERNAL company database.
Use this for:
- Employee information
- Customer data
- Company records
Do NOT use for:
- External information (use search_web instead)
"""
pass2. Update system prompt with examples:
system_prompt="""
Use search_web for: "What's the weather?" "Latest news about AI"
Use search_database for: "Find employee John" "Customer records"
"""3. Use better model:
# gpt-4 is better at tool selection
agent = Agent("assistant", model="gpt-4o", tools=[...])Symptom: Tool gets incorrect or missing parameters
Solutions:
1. Add clear parameter descriptions:
def send_email(to: str, subject: str, body: str) -> str:
"""
Send an email.
Args:
to: Recipient email address (e.g., "user@example.com")
subject: Email subject line (e.g., "Meeting Reminder")
body: Full email message content
Returns:
Success or error message
"""
pass2. Add parameter validation with helpful errors:
def send_email(to: str, subject: str, body: str) -> str:
"""Send an email"""
if "@" not in to:
return f"Error: '{to}' is not a valid email address. Format: user@example.com"
if not subject:
return "Error: Email must have a subject line"
if len(body) < 10:
return "Error: Email body too short. Please provide a complete message."
# Send email...3. Provide examples in docstring:
def schedule_meeting(date: str, time: str, attendees: str) -> str:
"""
Schedule a meeting.
Args:
date: Date in YYYY-MM-DD format (e.g., "2024-03-15")
time: Time in HH:MM format (e.g., "14:30")
attendees: Comma-separated emails (e.g., "john@email.com, jane@email.com")
Example:
schedule_meeting("2024-03-15", "14:30", "john@email.com, jane@email.com")
"""
passCauses & Solutions:
1. Tool returns unclear information:
# ✗ Unclear result
def search(query):
return data # Raw data dump
# ✓ Formatted result
def search(query):
results = api.search(query)
return f"""
Found {len(results)} results for '{query}':
1. {results[0].title}
{results[0].summary}
2. {results[1].title}
{results[1].summary}
"""2. Need better system prompt:
system_prompt="""
You are a research assistant. Follow these rules:
1. Always cite sources when presenting information
2. If unsure, say "I'm not certain" instead of guessing
3. Use the search tool for current information
4. Verify facts before presenting them
"""3. Use better model:
# gpt-4 is more accurate than gpt-4o-mini
agent = Agent("assistant", model="gpt-4o")Symptom: Agent keeps calling the same tool repeatedly
Causes & Solutions:
1. Tool returns unclear success/failure:
# ✗ Unclear result
def send_email(to: str) -> str:
smtp.send(to)
return "Done" # Agent can't tell if it succeeded
# ✓ Clear result
def send_email(to: str) -> str:
try:
smtp.send(to)
return f"✓ SUCCESS: Email sent to {to}"
except Exception as e:
return f"✗ FAILED: Could not send email to {to}. Error: {str(e)}"2. Add iteration limit:
# Default is 10, but you can change it
agent = Agent(
"assistant",
tools=[...],
max_iterations=5 # Stop after 5 tool calls
)3. Debug with auto_debug():
from connectonion.decorators import xray
@xray
def problematic_tool():
pass
agent.auto_debug()
agent.input("Task that causes loop")
# See what's happening at each iterationSolutions:
1. Use faster model:
# gpt-4o-mini is much faster than gpt-4
agent = Agent("assistant", model="gpt-4o-mini")2. Reduce tool complexity:
# ✗ Slow - processes everything
def search(query):
results = api.search(query)
for r in results:
r.analyze() # Slow!
return results
# ✓ Fast - only return what's needed
def search(query):
results = api.search(query, limit=5) # Limit results
return format_top_results(results) # Simple formatting3. Cache expensive operations:
from functools import lru_cache
@lru_cache(maxsize=100)
def expensive_search(query: str):
# Results cached for identical queries
time.sleep(2) # Slow operation
return results4. Reduce system prompt length:
# ✗ Very long prompt (slow)
system_prompt = """[5000 words of instructions]"""
# ✓ Concise prompt (fast)
system_prompt = """Be helpful and use tools when needed."""Solutions:
1. Install ConnectOnion:
pip install connectonion2. Verify installation:
pip list | grep connectonion3. Check Python version:
python --version # Must be 3.9+4. Use correct Python:
# If multiple Python versions:
python3.9 -m pip install connectonion
python3.9 your_script.pyCause: Optional dependency not installed
Solution:
pip install python-dotenvSymptom:
from connectonion import Agent # Works
from connectonion import auto_debug # ErrorSolution:
# Correct imports:
from connectonion import Agent
from connectonion.decorators import xray
# Then use:
agent = Agent("name")
agent.auto_debug() # Method, not functionSolutions:
1. Use cheaper model:
agent = Agent("assistant", model="gpt-4o-mini") # 60x cheaper than gpt-42. Shorter system prompts:
# ✗ Expensive - long prompt sent every time
system_prompt = """[Very long instructions]"""
# ✓ Cheaper - concise prompt
system_prompt = """Be helpful. Use tools when needed."""3. Limit tool descriptions:
# ✗ Very long docstring
def tool():
"""
[500 words explaining every detail]
"""
pass
# ✓ Concise but clear
def tool():
"""
Search the web for information.
Args:
query: Search query
"""
pass4. Monitor usage:
# Check token usage
response = agent.input("Task")
print(f"Tokens used: {agent.last_usage}") # If availableSolutions:
1. Clear conversation history:
# Agent keeps full conversation history
# Clear it periodically:
agent.current_session["messages"] = []2. Limit conversation turns:
# Start fresh agent for each task instead of long conversation:
for task in tasks:
agent = Agent("assistant", tools=[...])
agent.input(task)When something goes wrong:
-
Check API key
-
echo $OPENAI_API_KEYor check.envfile - Try key in a simple test:
import openai; openai.api_key="your-key"
-
-
Check tool docstrings
- Is the description clear?
- Are parameters documented?
- Are examples provided?
-
Enable auto_debug()
agent.auto_debug() # See exactly what's happening
-
Check model
- Try a better model:
model="gpt-4o" - Check if model is available in your region
- Try a better model:
-
Simplify
- Remove tools one by one
- Use minimal system prompt
- Test with simple task first
-
Check logs
- Look at console output
- Check
.co/logs/directory for agent logs
Still stuck?
-
Enable debug logging:
import logging logging.basicConfig(level=logging.DEBUG)
-
Check examples:
-
Community help:
-
Documentation:
Related Guides:
- Use Auto-Debug - Debug interactively
- Creating Custom Tools - Tool best practices
- FAQ - Common questions