What is an agent?
Agents vs chains vs pipelines
When you actually need an agent and when you don't.
Not everything needs an agent
Before you agent-ify everything, you should know when not to use one. Agents add complexity, latency, and unpredictability. If a simpler approach solves your problem, use it.
There are three common patterns for building with LLMs, and they sit on a spectrum of autonomy:
Pipelines: fixed steps, no decisions
A pipeline is a predefined sequence of steps. The LLM has no choice in what happens. You, the developer, decide the flow at build time.
# Pipeline: summarize → translate → format
def process_article(article):
summary = llm("Summarize this article: " + article)
translated = llm("Translate to Spanish: " + summary)
formatted = llm("Format as a bullet list: " + translated)
return formattedThe LLM is called three times, but it never decides anything about the flow. Step 1 always feeds into step 2, which always feeds into step 3. This is completely predictable.
Use a pipeline when:
- The steps are known ahead of time
- Every input follows the same path
- You need deterministic behavior (same input → same steps)
Examples: document processing, content transformation, data extraction from a known schema.
Chains: conditional steps, limited decisions
A chain adds branching. The LLM's output from one step determines which step comes next, but the possible paths are still defined by you.
# Chain: classify → route to the right handler
def handle_query(query):
category = llm("Classify this query as 'technical', 'billing', or 'general': " + query)
if "technical" in category:
return llm("You are a technical support expert. Help with: " + query)
elif "billing" in category:
return llm("You are a billing specialist. Help with: " + query)
else:
return llm("You are a general assistant. Help with: " + query)The LLM makes a classification decision, but the routing logic is yours. The model can't invent new categories or decide to do something outside the three paths you defined.
Use a chain when:
- There are a known set of possible paths
- The LLM needs to make a routing decision, but not a planning decision
- You want some flexibility without full autonomy
Examples: customer support routing, intent detection, conditional document processing.
Agents: open-ended decisions, dynamic planning
An agent has a goal and tools, and it decides at runtime what to do, in what order, and when to stop. You don't predefine the steps.
# Agent: figure it out yourself
def research_agent(question, tools):
return run_agent(
goal=f"Answer this question thoroughly: {question}",
tools=[search_web, read_page, take_notes, write_summary],
max_iterations=15,
)
# The agent might:
# 1. Search the web for the topic
# 2. Read 3 different pages
# 3. Take notes on conflicting information
# 4. Search again for a specific detail
# 5. Write a summary synthesizing everything
# ...or it might do something completely differentThe agent decides its own plan. It might take 2 steps or 12. It might use all four tools or just one. The path is determined at runtime based on what the model finds.
Use an agent when:
- The steps aren't known ahead of time
- The task requires dynamic planning and adaptation
- The number and type of actions depends on intermediate results
- You need the system to handle novel situations
Examples: research tasks, code debugging, data analysis with unknown schemas, multi-step workflows where the next step depends on what you find.
The decision framework
Here's a simple way to decide:
| Question | If yes → |
|---|---|
| Do you know every step at build time? | Pipeline |
| Do you know the possible paths but not which one? | Chain |
| Does the system need to plan its own steps? | Agent |
The complexity tradeoff
Each level adds power but also adds cost, latency, and unpredictability. An agent might take 10 LLM calls to do something a pipeline could do in 1. If a pipeline works, don't build an agent. If a chain works, don't build an agent. Only reach for agents when the task genuinely requires dynamic decision-making.
The real world is messy
In practice, production systems often combine all three. You might have a pipeline that preprocesses data, a chain that routes to the right specialist, and an agent that handles the complex cases. The skill isn't knowing how to build agents. It's knowing when to use each pattern.
In the next module, we'll dive deep into the orchestration loop (the core of every agent) and build it from scratch with proper error handling, exit conditions, and state management.
Done with this lesson?