Phase 5: AI Agents in Workflows · 55 min · LangChain · OpenAI API · Anthropic API
Agent Patterns — ReAct, Function Calling, and Tool Selection
Agents think, tools act, workflows orchestrate.
Hiring signal: Understanding agent patterns — when to use ReAct vs function calling, how to define tools, and when agents are overkill — is the skill that separates automation engineers from automation users. In interviews, being able to explain the C.H. Robinson agent architecture (LangChain + tools + order creation) demonstrates real agent production experience.
What you will learn
- Understand the ReAct pattern: Thought → Action → Observation → repeat until done
- Understand function calling: LLM selects a function and provides arguments from a defined schema
- Define tools for agents: name, description, parameters, and the function that executes
- Decide when to use agents vs fixed workflows — agents for dynamic decisions, workflows for known paths
The Problem
A logistics company receives 15,000 emails per day. Some are load offers that need to be created as orders in the TMS. But the path from email to order isn't fixed — sometimes the email has all the information (origin, destination, rate), sometimes it's missing the rate and the agent needs to look up historical rates, sometimes the carrier name is ambiguous and the agent needs to search the carrier database. A fixed workflow can't handle this variability.
An AI agent can. It reads the email, decides what information is missing, calls tools to look up the missing data, and creates the order — adapting its path based on what it finds. This is the ReAct pattern: the agent reasons about what to do next, takes an action, observes the result, and repeats until the task is complete.
Agents are for dynamic decisions; workflows are for known paths
If you know the exact steps in advance (trigger → classify → extract → act), use a fixed workflow. If the path depends on what the AI finds (missing data? look it up. Ambiguous? search database. Complete? create order), use an agent. Agents add flexibility but also cost, latency, and risk. Use them when you need adaptability, not because they're trendy.
The Concept
The ReAct Pattern
┌──────────────────────────────────────────────────────────────┐
│ ReAct: Reason + Act │
│ │
│ THOUGHT: "The email mentions a load from Chicago to Dallas │
│ but doesn't include the rate. I need to look up historical │
│ rates for this lane." │
│ ↓ │
│ ACTION: call lookup_rate(origin="Chicago", dest="Dallas") │
│ ↓ │
│ OBSERVATION: "Average rate for Chicago-Dallas: $2.50/mile, │
│ 850 miles = $2,125" │
│ ↓ │
│ THOUGHT: "I have the rate. Now I need to verify the │
│ carrier. The email says 'JB Trucking' — let me search." │
│ ↓ │
│ ACTION: call search_carrier(name="JB Trucking") │
│ ↓ │
│ OBSERVATION: "Found: J.B. Hunt Transport, MC# 242962" │
│ ↓ │
│ THOUGHT: "I have all required fields. Creating the order." │
│ ↓ │
│ ACTION: call create_order(origin="Chicago", dest="Dallas", │
│ rate=2125, carrier="J.B. Hunt", mc="242962") │
│ ↓ │
│ OBSERVATION: "Order created: ORD-123456" │
│ ↓ │
│ THOUGHT: "Task complete. Order ORD-123456 created." │
│ ↓ │
│ FINAL ANSWER: "Order created successfully: ORD-123456" │
└──────────────────────────────────────────────────────────────┘
Function Calling vs ReAct
| Aspect | Function Calling | ReAct Agent |
|---|
| How it works | LLM selects a function + arguments from a schema | LLM iterates: think → call tool → observe → repeat |
| Number of steps | One: call one function | Multiple: loop until done |
| Decision making | Single selection | Dynamic: decides next step based on observations |
| Use case | You know which function to call, just need arguments | You don't know the path; agent figures it out |
| Cost | One LLM call | Multiple LLM calls (one per iteration) |
| Latency | Low (one call) | High (multiple calls) |
| Reliability | High (structured) | Lower (agent may loop or hallucinate) |
A customer email says "I want to cancel order ORD-123456." The system needs to: look up the order, check if it's cancellable, cancel it, and send confirmation. Should this use function calling or a ReAct agent?
A ReAct agent is better here because the path is dynamic: the agent needs to look up the order first, then decide what to do based on the result. If the order is cancellable, it cancels and sends confirmation. If it's already shipped, it can't cancel and needs to explain why. If the order doesn't exist, it needs to say so. A fixed workflow with function calling would need to handle all these branches explicitly, while the agent adapts naturally. However, if the cancellation rules are simple and known, a fixed workflow with IF nodes is cheaper and more reliable.
Tool Definition Anatomy
# A tool has four parts:
tool = {
"name": "lookup_rate", # Unique identifier
"description": "Look up historical freight rate for a lane", # When to use this
"parameters": { # What the LLM needs to provide
"type": "object",
"properties": {
"origin": {"type": "string", "description": "Origin city"},
"destination": {"type": "string", "description": "Destination city"}
},
"required": ["origin", "destination"]
},
"function": lookup_rate # The actual code that runs
}
def lookup_rate(origin: str, destination: str) -> dict:
"""Look up historical freight rate."""
# Database query
return {"rate_per_mile": 2.50, "distance": 850, "total": 2125}
When to Use Agents vs Fixed Workflows
| Factor | Fixed Workflow | Agent |
|---|
| Path is known | ✅ Yes — define steps explicitly | ❌ Overkill |
| Path is dynamic | ❌ Can't adapt | ✅ Agent adapts |
| Cost sensitivity | ✅ One AI call | ❌ Multiple AI calls |
| Latency matters | ✅ Fast | ❌ Slow (multiple iterations) |
| Reliability critical | ✅ Deterministic | ❌ May loop or hallucinate |
| Complex decision logic | ❌ Hard to encode all branches | ✅ Agent reasons |
Unlock the full lesson
You've read the first 2 sections. The rest of this lesson covers Build It, Use It, Ship It, Exercises, Key Terms, Common Pitfalls — plus a hands-on lab, quiz, and project artifact.
Create a free account to unlock Phase 0 and Phase 1 of every course — no credit card.
Browse all courses · View pricing · DeVenture Academy