Agent Building
Build autonomous AI agents and pipelines
AI agents are autonomous systems that perceive their environment, reason about goals, and take actions using tools to accomplish tasks. Modern agent architectures combine large language models with function calling, memory systems, and orchestration frameworks to enable multi-step problem solving. Understanding agent design patterns, from simple tool-using loops to sophisticated multi-agent systems, is essential for building reliable, production-grade AI applications.
Key formulas and rules
Key concepts
Agentic Loops and the ReAct Pattern
The core of modern AI agents is the agentic loop-a cycle of reasoning, acting, and observing. The ReAct (Reasoning + Acting) pattern interleaves thoughts (internal reasoning) with actions (tool calls) and observations (tool outputs). This pattern enables agents to break down complex tasks, make decisions dynamically, and recover from errors. The loop continues until the agent determines the task is complete or reaches a maximum iteration limit. Key considerations include preventing infinite loops, managing token budgets, and ensuring the agent has sufficient context to make good decisions at each step.
Function Calling and Tool Use
Function calling allows LLMs to invoke external tools through structured outputs. Modern APIs (OpenAI, Anthropic, Google) support native function calling where the model outputs a function name and arguments in a structured format. Tools are defined with JSON schemas specifying their name, description, and parameter types. The agent runtime parses the function call, executes it, and returns the result as an observation. Best practices include writing clear tool descriptions, validating inputs before execution, handling errors gracefully, and limiting the number of available tools to prevent choice paralysis. Tools can be anything from simple calculators to complex API integrations, database queries, or file system operations.
Multi-Agent Orchestration
Multi-agent systems coordinate multiple specialized agents to solve complex problems. Common patterns include: (1) Hierarchical-supervisor agent delegates tasks to worker agents; (2) Sequential-agents process in a pipeline where each agent's output feeds the next; (3) Parallel-multiple agents work independently on subtasks, results are aggregated; (4) Debate-agents with different perspectives argue to reach consensus. Frameworks like LangChain's LangGraph, CrewAI, AutoGen, and Anthropic's Claude Agent SDK provide abstractions for building these systems. Key challenges include managing inter-agent communication, handling conflicting outputs, and ensuring the overall system remains debuggable. Each agent should have a clear role and expertise domain.
Memory Systems: Short-term and Long-term
Agents require memory to maintain context across conversations and tasks. Short-term memory (working memory) holds recent conversation history and is typically managed through the message array passed to the LLM. Long-term memory persists information across sessions using vector databases (Pinecone, Chroma, pgvector) for semantic search. Memory architectures include: (1) Episodic-records of past experiences and actions; (2) Semantic-facts and knowledge extracted from interactions; (3) Procedural-learned skills and successful action sequences. Effective memory systems balance retrieval relevance with token efficiency, using techniques like summarization, chunking, and relevance scoring to surface only the most useful context.
Worked examples
Example 1
Design a customer support agent that can look up order information, process refunds, and escalate to humans. Define the necessary tools and the agentic loop.
Step 1: Define the tools the agent needs.
- lookup_order(order_id: str) → Returns order details, status, items, and history.
- process_refund(order_id: str, amount: float, reason: str) → Initiates refund, returns confirmation or error.
- create_ticket(user_id: str, issue_summary: str, priority: str) → Creates escalation ticket, returns ticket ID.
- check_inventory(product_id: str) → Checks if replacement item is available.
Step 2: Define the agent loop (ReAct pattern).
1. Receive user query with authentication context (user_id).
2. THOUGHT: Analyze the query to determine intent and required information.
3. ACTION: Call appropriate tool(s) based on intent.
4. OBSERVATION: Receive tool output.
5. THOUGHT: Evaluate if the result satisfies the user's need or if more actions are needed.
6. If task complete: Respond to user with summary.
7. If stuck after 3 iterations: Escalate to human using create_ticket.
Step 3: Add guardrails.
- Require order_id verification before processing refunds.
- Limit refund amount without supervisor approval (e.g., $500 threshold).
- Log all actions for audit trail.
- Maximum 10 tool calls per conversation to prevent runaway loops.
Example 2
A research agent needs to answer questions by searching the web and synthesizing findings. Implement a multi-hop retrieval pattern where the agent can follow citations.
Step 1: Define the tools.
- web_search(query: str) → Returns top search results with URLs and snippets.
- fetch_page(url: str) → Returns full text content of a webpage.
- extract_citations(text: str) → Returns URLs referenced in the text.
Step 2: Implement the multi-hop retrieval loop.
1. Initial query: User asks 'What are the latest advances in quantum error correction?'
2. ACTION: web_search('latest advances quantum error correction 2025')
3. OBSERVATION: Results include academic papers, news articles, and blog posts.
4. THOUGHT: The top result from Nature looks most authoritative. Let me read it.
5. ACTION: fetch_page(nature_article_url)
6. OBSERVATION: Article discusses new surface code implementations and cites relevant papers.
7. THOUGHT: This paper cites the original surface code paper-I should read that for context.
8. ACTION: extract_citations(current_text) → Returns URLs including the foundational paper.
9. ACTION: fetch_page(foundational_paper_url)
10. OBSERVATION: Explains the theoretical basis for surface codes.
11. THOUGHT: Now I have enough context to synthesize a comprehensive answer.
12. OUTPUT: Summarize findings with citations to both sources.
Step 3: Add stopping conditions.
- Maximum 5 hops to prevent infinite citation chasing.
- Track visited URLs to avoid re-fetching.
- Use embedding similarity to assess when gathered information is sufficient.
Representative solved questions
See the kind of question in this topic before opening the full practice set.
Question 1
What is the core characteristic of an agentic AI system compared to a standard chatbot?
It can autonomously take actions and use tools to achieve goals
It always uses larger language models
It only responds to user prompts without initiative
It requires less training data to operate
Answer: A. It can autonomously take actions and use tools to achieve goals
ExplanationStep 1: Understand the difference between passive and active AI systems.
Step 2: Standard chatbots respond reactively to user inputs.
Step 3: Agentic systems can plan, reason, and take autonomous actions using tools.
Answer: Agentic AI autonomously takes actions and uses tools to achieve goals.
Sources and review notes
This is an AISEA-authored practice question.
Review status: accepted · Reviewed 2026-08-13 · structure and answer-key checks, editorial quality checks, duplicate screening
Question 2
In the ReAct pattern for agents, what does 'Re' stand for?
Retrieval
Recursive
Reasoning
Response
Answer: C. Reasoning
ExplanationStep 1: ReAct is a prompting pattern combining reasoning and acting.
Step 2: The name is a portmanteau of 'Reasoning' and 'Acting'.
Step 3: Agents interleave thought traces with action execution.
Answer: 'Re' stands for Reasoning.
Sources and review notes
This is an AISEA-authored practice question.
Review status: accepted · Reviewed 2026-08-13 · structure and answer-key checks, editorial quality checks, duplicate screening
Question 3
What is the primary purpose of function calling in LLM-based agents?
To reduce the number of tokens in the prompt
To encrypt sensitive data before transmission
To automatically fine-tune the model during runtime
Common mistakes and useful habits
- Always set a maximum iteration limit for agent loops-without it, an agent can get stuck in an infinite reasoning cycle, consuming tokens indefinitely. A good starting point is 10–20 iterations depending on task complexity.
- Write tool descriptions as if explaining to a human colleague-the LLM relies entirely on these descriptions to decide when and how to use each tool. Include preconditions, parameter constraints, and examples of good inputs.
- Implement structured output parsing for tool calls-don't rely on regex to extract function names from free text. Use native function calling APIs or JSON-mode to guarantee parseable outputs.
- Log every agent action, thought, and observation with timestamps. This audit trail is invaluable for debugging why an agent made a particular decision and for improving prompts.
- When using RAG with agents, let the agent control retrieval rather than doing a single upfront fetch. The agent can decide when it has enough information or when it needs to search differently.
- For multi-agent systems, keep each agent focused on a narrow domain of expertise. A 'master of all trades' agent often performs worse than several specialized agents that collaborate.
Ready to test your understanding?
Work through 100 questions with explanations after each answer.