AI Agents & Orchestration
Build autonomous AI agents with the ReAct pattern, tool schemas, memory management, multi-agent systems, and safety guardrails.
Anatomy of an AI Agent
An AI agent is an LLM in a loop that can reason about tasks and use tools to accomplish them.
The ReAct Loop
while not done:
thought = llm.think(task, history, tools)
if thought.is_final_answer:
return thought.answer
action = thought.tool_call
observation = execute_tool(action)
history.append(thought, action, observation)
Each iteration:
- Thought: The model reasons about the current state and what to do next
- Action: It chooses a tool and provides arguments
- Observation: The tool executes and returns results
- Loop: The model incorporates the result and decides the next step
Tool Definition
Tools need clear, precise schemas:
{
"name": "search_database",
"description": "Search the product database by query. Returns top 5 matching products with name, price, and availability.",
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "Search query β product name, category, or description keywords"
},
"max_price": {
"type": "number",
"description": "Optional maximum price filter in USD"
}
},
"required": ["query"]
}
}
Critical tips:
- Descriptions matter more than names β the model reads them to decide when/how to use tools
- Include examples in descriptions for complex parameters
- Validate arguments before executing β the model can hallucinate invalid values
Memory Architecture
Agents need different types of memory:
Conversation Memory
The full message history. For long conversations, summarise older messages to stay within context limits.
Scratchpad (Working Memory)
Short-term notes for the current task:
Plan: 1) Search for user's order 2) Check return eligibility 3) Process refund
Step 1 result: Found order #4521, shipped March 3rd
Step 2 result: Within 30-day return window, eligible
Current: Processing refund...
Long-Term Memory
Persistent storage across sessions β user preferences, past interactions, learned facts. Typically stored in a database and retrieved as needed.
Multi-Agent Orchestration
Complex tasks benefit from multiple specialised agents:
Orchestrator Pattern
A βmanagerβ agent delegates subtasks to specialist agents:
- Research agent β gathers information
- Analysis agent β evaluates findings
- Writer agent β produces the final output
Pipeline Pattern
Agents work in sequence, each transforming the output of the previous:
Input β Agent A (research) β Agent B (analysis) β Agent C (writing) β Output
Debate Pattern
Multiple agents with different perspectives discuss and converge on an answer β useful for reducing bias and improving reasoning.
Planning Patterns
Plan-then-Execute
The agent creates a full plan before acting:
- Generate step-by-step plan
- Execute each step
- Revise plan if a step fails
Dynamic Planning
Re-plan after each step based on new information. More flexible but can be less efficient.
Safety Guardrails
Must-Have Guardrails
-
Action confirmation β Require human approval for irreversible actions (send email, delete data, make purchases)
-
Scope limits β Restrict what tools are available. A customer support agent shouldnβt have access to the database admin tool.
-
Rate limits β Cap the number of tool calls per task to prevent runaway loops
-
Output filtering β Check agent outputs for harmful content, PII leakage, or off-topic responses before delivering to users
-
Maximum iterations β Set a hard limit on the agent loop to prevent infinite loops
The Human-in-the-Loop Pattern
For high-stakes actions:
if action.risk_level == "high":
approval = await request_human_approval(action)
if not approval:
agent.replan()
Common Failure Modes
- Tool call loops β Agent repeatedly calls the same tool expecting different results
- Hallucinated tool arguments β Model invents parameter values
- Lost context β Long agent runs exceed context window
- Error cascading β One failed step derails the entire task
- Scope creep β Agent takes actions outside its intended domain
Key takeaway: Agent engineering is about controlled autonomy β giving the AI enough freedom to be useful while maintaining guardrails that prevent costly mistakes.
Quick Quiz
Test what you just learned. Pick the best answer for each question.
Q1 What is the ReAct pattern?
Q2 Why do agents need a well-defined tool schema?
Q3 What is the 'scratchpad' pattern in agent memory?
Q4 What's the primary safety concern with autonomous AI agents?