An AI agent is a system where a language model autonomously decides what to do next — which tools to call, what information to gather, how to handle errors — to accomplish a goal. Building one that works reliably in production requires understanding the architecture, not just the API calls.
1. What makes something an agent (vs a workflow)
In a workflow, you orchestrate the steps in code. In an agent, the LLM decides the steps. The difference matters: agents can handle tasks that are hard to fully specify in advance, adapt to unexpected situations, and self-correct. But they're also less predictable, more expensive, and harder to debug. Use agents only when the task genuinely requires dynamic decision-making.
- Workflow: LLM is called in a fixed sequence you define. Predictable, cheap, easy to test.
- Agent: LLM decides the next step each turn. Flexible, expensive, harder to test.
- Most "agents" in production are actually hybrid: fixed outer pipeline + LLM-driven inner steps
- Default to workflows; reach for agents when the task can't be fully specified in advance
2. The ReAct loop
The most common agent architecture is ReAct (Reason + Act). The model is given a goal and a list of tools. Each turn: 1) Reason about what to do next, 2) Act by calling a tool, 3) Observe the result, 4) Repeat until done. The agent terminates when it produces a final_answer action or reaches a step limit. This simple loop underlies most production agents.
3. Tool design is the most important decision
Agent quality is bounded by tool quality. Good agent tools are specific, well-described, and return rich context. Bad tools are vague, return minimal information, or have confusing parameter names. Treat your tool descriptions like API documentation for the model — it's the primary way you communicate intent. Test each tool individually before testing the full agent.
Tip
The description should answer: what does this tool do, when should I use it, and what does it return? A one-line description is usually not enough.
4. Memory: short-term, long-term, and episodic
By default, agents only have the current conversation context as memory. For tasks requiring information from earlier in a session (short-term) or from previous sessions (long-term), you need explicit memory management. Common patterns: append tool results to conversation history (short-term), store summaries in a database and retrieve via a search tool (long-term), use embeddings for semantic search over past interactions (episodic).
5. Handling errors and uncertainty
Production agents encounter failure constantly: tool timeouts, ambiguous instructions, contradictory information, loops. Design for failure from the start. Return errors as tool results so the model can decide how to proceed. Add a 'clarify' tool for cases where the model is uncertain. Set max steps per task. Log all tool calls and model decisions — you'll need the logs to debug production failures.
- Max steps: hard limit on tool calls per task (e.g. 20)
- Error tool results: return structured errors the model can reason about
- Clarification tool: ask the user when genuinely ambiguous
- Fallback answer: if max steps reached, explain what was accomplished and what was not
- Human-in-the-loop: for irreversible actions, require explicit confirmation
6. Multi-agent architectures
For complex tasks, a single agent loop isn't enough. Orchestrator-subagent patterns assign specialised sub-agents to subtasks (a research agent, a writer agent, a code agent) while an orchestrator manages the overall workflow. This reduces context window pressure, allows specialisation, and enables parallelism. Claude Opus 4.8 and GPT-4o work well as orchestrators; faster/cheaper models can run as subagents.
7. Testing and evaluation
Agent testing is fundamentally different from unit testing. You can't test the full agent loop the same way you test a function. Approaches: trace-based testing (record tool call sequences from golden runs, verify new runs match the pattern), LLM-as-judge (have another model evaluate the final output quality), and adversarial testing (test with inputs designed to confuse or loop the agent). The most important signal is task completion rate on a held-out eval set.
Tip
Log every tool call, model decision, and intermediate result to a persistent store. Without traces, debugging a failed agent run is nearly impossible. Tools like LangSmith and OpenTelemetry can help.
8. Model selection for agents
Not all models are equal for agentic tasks. Claude Opus 4.8 and GPT-4o lead BFCL benchmarks and show the best real-world task completion on complex multi-step tasks. Claude Sonnet 4.6 is the best cost-to-performance option for production. Gemini 2.0 Flash is worth testing for cost-sensitive pipelines. For latency-sensitive agents (< 500ms tool call overhead), use the fastest model that meets your quality bar.
