Workflow-004: Agents & Agentic Workflows
FreshSource: Building with the Claude API
Document Control
| Field | Value |
|---|---|
| Workflow ID | WF-004 |
| Version | 1.0 |
| Status | Active |
| Last Updated | 2024-12 |
Overview
Build autonomous agents and multi-step agentic workflows with Claude.
Agent Architecture
Agent Types
| Pattern | Description | Good For |
|---|---|---|
| Simple Agent (ReAct) | Single model with tools, Reason/Act/Observe loop | Focused, single-domain tasks |
| Orchestrator-Worker | Orchestrator plans, workers execute, Parallel execution possible | Complex, multi-step tasks |
| Router Pattern | Route to specialized agents, Each agent has domain expertise | Multi-domain applications |
Simple Agent Implementation
python
import anthropic
client = anthropic.Anthropic()
def agent_loop(goal, tools, max_iterations=10):
messages = [{"role": "user", "content": goal}]
for i in range(max_iterations):
response = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=4096,
tools=tools,
messages=messages
)
# Check if done
if response.stop_reason == "end_turn":
return extract_final_response(response)
# Process tool calls
if response.stop_reason == "tool_use":
tool_results = execute_tools(response.content)
messages.append({"role": "assistant", "content": response.content})
messages.append({"role": "user", "content": tool_results})
return "Max iterations reached"Orchestrator-Worker Pattern
python
def orchestrator_workflow(task):
# Step 1: Plan
plan = client.messages.create(
model="claude-sonnet-4-20250514",
system="You are a planner. Break down tasks into steps.",
messages=[{"role": "user", "content": f"Plan: {task}"}]
)
steps = parse_plan(plan.content[0].text)
# Step 2: Execute each step with workers
results = []
for step in steps:
worker_result = execute_worker(step)
results.append(worker_result)
# Step 3: Synthesize
synthesis = client.messages.create(
model="claude-sonnet-4-20250514",
system="Synthesize these results into a final answer.",
messages=[{
"role": "user",
"content": f"Task: {task}\n\nResults: {results}"
}]
)
return synthesis.content[0].textRouter Pattern
python
def router_agent(query):
# Classify the query
classification = client.messages.create(
model="claude-haiku-3-20240307",
system="""Classify queries into:
- code: Programming questions
- research: Information lookup
- creative: Writing/creative tasks
Respond with just the category.""",
messages=[{"role": "user", "content": query}]
)
category = classification.content[0].text.strip()
# Route to specialized agent
agents = {
"code": code_agent,
"research": research_agent,
"creative": creative_agent
}
return agents.get(category, default_agent)(query)Agentic Guardrails
| Guardrail | Measures |
|---|---|
| Input Validation | Validate all tool inputs, Sanitize user-provided data, Reject malformed requests |
| Execution Limits | Max iterations per task, Timeout per operation, Resource usage caps |
| Action Review | Human-in-the-loop for sensitive actions, Audit logging, Rollback capabilities |
Verification Checklist
- [ ] Agent loop terminates properly
- [ ] Tool execution handles errors
- [ ] Iteration limits enforced
- [ ] Results validated before return
- [ ] Logging captures all steps
- [ ] Guardrails in place