📖 You are viewing: Conceptual Guide - Learn how agents work and build your first agentSee also: API Specification · Quickstart Tutorial
Source Code:
src/gaia/agents/base/agent.pyWhat You’ll Learn
- What an agent is and why it’s more powerful than calling an LLM directly
- How the agent reasoning loop works under the hood
- How to build your first agent from scratch
- How to configure agents for different use cases
- Common pitfalls and how to avoid them
What is an Agent?
An agent is more than just an LLM. Think of the difference like this:
Real-world analogy: An LLM is like a brilliant consultant who can only give advice. An agent is that same consultant, but now they have a phone, a computer, and access to your company’s systems—they can actually do the work.
Why Use Agents?
- Without Agent
- With Agent
How Agents Work: The Reasoning Loop
When you send a message to an agent, it doesn’t just generate a response. It enters a reasoning loop:Key Insight: The loop can repeat! If the LLM decides it needs more information after step 5, it goes back to step 3 and executes another tool. This enables complex multi-step reasoning.
Agent States
During processing, agents transition through different states:
You can check the current state in your tools if you need conditional behavior:
Building Your First Agent
Let’s build an agent step by step, starting simple and adding features progressively.Step 1: The Minimal Agent
Every agent needs three things:- A system prompt - Tells the LLM who it is
- A console - Handles output display
- Tools - What the agent can do
- Agent receives “Hello! What can you help me with?”
- LLM sees: System prompt + User message
- LLM generates a conversational response
- Agent returns the response
Step 2: Adding Your First Tool
Let’s give the agent the ability to tell time:- User asks “What time is it?”
- LLM sees the
get_current_timetool and its description - LLM decides: “This matches ‘What time is it?’ - I should use this tool”
- Agent executes
get_current_time(), gets{"time": "2:30 PM", ...} - LLM receives the result and generates a natural response
Step 3: Adding External Capabilities
Now let’s build something more practical—an agent that can fetch weather data:- Instance variables (
self.api_key) - Store configuration in__init__ - Tool parameters with defaults (
country_code="US") - LLM learns optional params - Error handling - Return error info, don’t raise exceptions
- Timeout handling - External APIs can be slow
- Rich system prompt - Guides LLM behavior beyond just tool selection
Configuration Deep Dive
Agents accept many configuration parameters. Here’s what each one does:Choosing Your LLM Backend
Option 1: Local LLM (Default) - Free, Private, Fast
Option 1: Local LLM (Default) - Free, Private, Fast
Uses AMD-optimized Lemonade Server running on your machine:Pros:
- Free (no API costs)
- Data stays on your machine (privacy)
- Fast inference on AMD hardware (NPU/iGPU acceleration)
- Requires Lemonade Server running
- Smaller models than cloud options
- Local compute resources needed
Option 2: Claude API - Best Reasoning
Option 2: Claude API - Best Reasoning
Uses Anthropic’s Claude models:Pros:
- Excellent reasoning and code understanding
- Large context window
- Most capable for complex tasks
- API costs (pay per token)
- Requires internet connection
- Data sent to Anthropic
Option 3: ChatGPT API - Wide Compatibility
Option 3: ChatGPT API - Wide Compatibility
Uses OpenAI’s GPT models:Pros:
- Wide tool support
- Familiar API
- Good general performance
- API costs
- Requires internet connection
Tuning Agent Behavior
Common Pitfalls and Solutions
Pitfall 1: Agent doesn't use my tools
Pitfall 1: Agent doesn't use my tools
Symptom: Agent gives generic responses instead of using your tools.Cause: Tool docstrings don’t clearly explain when to use them.
Pitfall 2: Agent gets stuck in loops
Pitfall 2: Agent gets stuck in loops
Symptom: Agent keeps calling tools repeatedly without making progress.Cause: Usually means
max_steps is too high, or tools return ambiguous results.Solutions:Pitfall 3: Tool errors crash the agent
Pitfall 3: Tool errors crash the agent
Symptom: Agent stops working when a tool encounters an error.Cause: Raising exceptions in tools instead of returning error information.
Pitfall 4: Context getting too long
Pitfall 4: Context getting too long
Symptom: Agent becomes slow or starts giving inconsistent responses.Cause: Conversation history or tool results exceeding context limits.Solutions:
Practice Challenge
Build a File Explorer Agent
Create an agent that can:
Why this solution works:
- List files in a directory
- Read file contents
- Search for text in files
- Handle errors gracefully (missing files, permission denied)
- Limit file content to prevent context overflow
- Clear tool docstrings that guide the LLM
Hints
Hints
- Use
os.listdir()for listing files - Use
os.path.isfile()to check if path is a file - Catch
FileNotFoundErrorandPermissionError - Return structured dicts with status fields
Solution
Solution
- Security: Validates paths stay within allowed directory
- Error handling: Returns informative error dicts instead of raising
- Context limits: Truncates large results to prevent overflow
- Clear docstrings: LLM knows exactly when to use each tool
- Configurable:
allowed_pathrestricts scope for safety
Deep Dive: Under the Hood
How process_query() Works Internally
How process_query() Works Internally
When you call Key insight: The conversation history grows with each tool call, giving the LLM more context for its next decision.
agent.process_query(user_input), here’s the detailed flow:How Tool Registration Works
How Tool Registration Works
The The function name, type hints, and docstring are what the LLM sees, which is why
they matter so much. See the Tools guide for the authoritative
walkthrough of the registry entry.
@tool decorator does several things:Key Takeaways
Agents = LLM + Tools + Memory
Agents can plan, act, observe, and reason—not just generate text.
Three Required Methods
Every agent needs:
_get_system_prompt(), _create_console(), _register_tools()Docstrings Teach the LLM
The LLM only sees your docstring—make it clear when to use each tool.
Return Errors, Don't Raise
Tools should return error info as data so the agent can recover gracefully.
Next Steps
Tool Decorator
Deep dive into creating effective tools
Console Output
Customize how your agent displays output
User Guides
See pre-built agents for chat, code, Jira, Blender