Building AI Agents: Architecture Patterns and Practical Examples
What Is an AI Agent?
An AI agent is a system where an LLM acts as the reasoning engine, deciding what actions to take based on observations and goals. Unlike a chatbot (which responds to a single prompt), an agent operates in a loop: observe, think, act, observe the result, think again.
The Core Loop
while not task_complete:
observation = get_current_state()
thought = llm.reason(observation, goal, history)
action = thought.next_action()
result = execute(action)
history.append(observation, thought, action, result)
Architecture Patterns
1. ReAct (Reasoning + Acting)
The most common pattern. The agent alternates between reasoning steps (thinking about what to do) and action steps (executing tools). Each reasoning step explicitly articulates the agent’s thought process.
For more on this topic, see our guide on How to Automate Unit Testing with AI Tools.
Thought: I need to find the user's order status. I'll query the database.
Action: query_database(order_id="12345")
Observation: Order 12345: shipped, tracking: 1Z999AA10123456784
Thought: I have the tracking info. I'll format a response.
Action: respond("Your order shipped! Tracking: 1Z999AA10123456784")
2. Plan-and-Execute
The agent creates a complete plan upfront, then executes each step sequentially. Better for complex, multi-step tasks where the order of operations matters.
For more on this topic, see our guide on Multi-File Editing AI Agents Compared: Cursor, Claude Code, Copilot Workspace, and More.
3. Multi-Agent
Multiple specialized agents collaborate. One agent handles research, another handles code generation, a third handles review. A coordinator agent delegates tasks and synthesizes results.
Tool Design Principles
- Clear, descriptive names:
search_databasenottool_3 - Typed parameters: The LLM needs to know what inputs each tool expects
- Informative return values: Return enough context for the agent to reason about the result
- Error handling: Return error messages the agent can understand and recover from
- Idempotency: Tools should be safe to retry without side effects
Common Failure Modes
- Loops: Agent gets stuck repeating the same action. Add a maximum iteration count.
- Hallucinated tools: Agent tries to use tools that do not exist. Constrain tool selection.
- Over-planning: Agent plans endlessly without acting. Force action after N reasoning steps.
- Context overflow: Long histories exceed the context window. Summarize periodically.