Skip to main content

🤝 AI Agents

Personal assistants that take action

The Executive Assistant Analogy

An executive assistant doesn't just answer questions - they get things done. You say "book a flight to Sydney next week," and they check your calendar, search flights, compare options, and make the booking.

AI Agents work the same way.

Instead of just generating text, agents can take actions. They plan, use tools, observe results, and iterate until the task is complete. They're LLMs with hands.


Agents vs Basic LLMs

Basic LLMAI Agent
Answers questionsCompletes tasks
Single turnMultiple turns
No external actionsUses tools
StatelessMaintains context
ReactiveProactive

Example

Basic LLM:

User:   "What's the weather in Sydney?"
LLM:    "I don't have access to real-time weather data."

Agent with tools:

User:   "What's the weather in Sydney?"
Agent:  [Thinks: I need to use the weather tool]
        [Action: call weather_api("Sydney")]
        [Observation: 22°C, sunny]
Agent:  "It's currently 22°C and sunny in Sydney."

How Agents Work

The Agent Loop

┌──────────────────────────────────────────────────────────┐
│                        LOOP                              │
│                                                          │
│  ┌─────────┐    ┌─────────┐    ┌─────────┐              │
│  │  Think  │ -> │  Plan   │ -> │   Act   │              │
│  └─────────┘    └─────────┘    └────┬────┘              │
│       ↑                             │                    │
│       │         ┌─────────┐         │                    │
│       └─────────│ Observe │<────────┘                    │
│                 └─────────┘                              │
│                                                          │
└──────────────────────────────────────────────────────────┘
  1. Think: Reason about the task
  2. Plan: Decide what action to take
  3. Act: Execute a tool or function
  4. Observe: See the result
  5. Repeat: Until task is complete

Tool Calling

Tools give agents capabilities beyond text generation:

const tools = [
  {
    name: "search_web",
    description: "Search the web for current information",
    parameters: {
      query: { type: "string", description: "Search query" },
    },
  },
  {
    name: "calculator",
    description: "Perform mathematical calculations",
    parameters: {
      expression: { type: "string", description: "Math expression" },
    },
  },
  {
    name: "send_email",
    description: "Send an email to a recipient",
    parameters: {
      to: { type: "string" },
      subject: { type: "string" },
      body: { type: "string" },
    },
  },
];

Agent Execution

def run_agent(user_request, tools):
    messages = [{"role": "user", "content": user_request}]

    while True:
        # Get LLM response
        response = llm.chat(messages, tools=tools)

        # Check if done
        if response.type == "final_answer":
            return response.content

        # Execute tool call
        if response.type == "tool_call":
            tool_name = response.tool_name
            tool_args = response.tool_args

            result = execute_tool(tool_name, tool_args)

            # Add observation to context
            messages.append({
                "role": "tool",
                "content": result
            })

Common Agent Patterns

ReAct (Reasoning + Acting)

Agent explains its reasoning before each action:

Thought: I need to find the current price of Bitcoin
Action: search_web("bitcoin price USD today")
Observation: [current price returned by search]
Thought: Now I have the information to answer
Final Answer: Bitcoin is currently priced at $X USD.

Plan-and-Execute

Make a plan first, then execute step by step:

Plan:
1. Search for flight options
2. Compare prices
3. Check user's calendar
4. Book an option that fits the criteria

Executing Step 1...
Executing Step 2...

Multi-Agent Systems

Multiple specialized agents collaborating:

Researcher Agent: Gathers information
Analyst Agent: Evaluates options
Writer Agent: Produces final output
Coordinator: Manages workflow

Real-World Examples

1. Research Assistant

agent.run("""
Research the top 3 electric vehicles right now.
Compare their range, price, and charging speed.
Write a summary report.
""")

# Agent actions:
# 1. search_web("top electric vehicles")
# 2. search_web("Tesla Model 3 specs")
# 3. search_web("BMW i5 specs")
# 4. search_web("Hyundai Ioniq 6 specs")
# 5. write_report(gathered_data)

2. Code Assistant

agent.run("""
There's a bug in the login function.
Find it and fix it.
""")

# Agent actions:
# 1. read_file("auth.py")
# 2. analyze_code(file_content)
# 3. identify_bug("missing password hash verification")
# 4. write_fix(corrected_code)
# 5. run_tests()

Common Mistakes and Gotchas

Insufficient Tool Descriptions

# Bad - vague description
{
    "name": "search",
    "description": "Search for stuff"
}

# Good - specific and helpful
{
    "name": "search_web",
    "description": "Search the web for current information. Use for recent events, prices, or facts not in training data."
}

No Exit Condition

# Agent loops forever
while True:
    response = run_step()
    # Missing: check for completion

# Add exit conditions
if response.is_final or steps > max_steps:
    break

Over-reliance on Agent Decisions

Agents can make mistakes. Implement guardrails:

# Confirm before destructive actions
if action.is_destructive:
    await confirm_with_user(action)

Not Handling Failures

try:
    result = execute_tool(tool_name, args)
except ToolError as e:
    # Let agent know and retry
    messages.append({
        "role": "tool",
        "content": f"Error: {e}. Please try a different approach."
    })

FAQ

Q: What is the difference between RAG and agents?

RAG retrieves information to augment responses. Agents use tools to take actions. An agent might use RAG as one of its tools.

Q: Can agents run autonomously?

Yes, but with guardrails. Fully autonomous agents risk runaway behavior. Most production agents have human-in-the-loop for critical decisions.

Q: What about security?

Agents with tools can have real impact. Implement: sandboxing, rate limits, action confirmation, and minimal permissions.

Q: How do I choose what tools to give an agent?

Start minimal. Add tools as needed. Each tool should ideally have a clear, distinct purpose.

Q: What is function calling vs tool calling?

Same concept, different terminology. OpenAI calls it "function calling," others say "tool calling."

Q: How are agents better than just prompting?

Agents can complete multi-step tasks, access external systems, and adapt based on intermediate results. Prompting alone is limited to the model's training data.


Summary

AI agents extend LLMs from answering questions to completing tasks. They combine reasoning with the ability to use tools and take actions.

Key Points:

  • Agents follow a think-plan-act-observe loop
  • Tools give agents capabilities beyond text
  • ReAct pattern: reason before each action
  • Multi-agent systems coordinate specialized agents
  • Usually implement guardrails for safety
  • Clear tool descriptions improve reliability
  • Handle failures gracefully

Agents represent the next step in practical AI - systems that can actually do things, not just talk about them.

Leave a Comment

Comments (0)

Be the first to comment on this concept.

Comments are approved automatically.