Skip to content

Workflow-004: Agents & Agentic Workflows

Fresh

Source: Building with the Claude API

Document Control

FieldValue
Workflow IDWF-004
Version1.0
StatusActive
Last Updated2024-12

Overview

Build autonomous agents and multi-step agentic workflows with Claude.

Agent Architecture

Agent Types

PatternDescriptionGood For
Simple Agent (ReAct)Single model with tools, Reason/Act/Observe loopFocused, single-domain tasks
Orchestrator-WorkerOrchestrator plans, workers execute, Parallel execution possibleComplex, multi-step tasks
Router PatternRoute to specialized agents, Each agent has domain expertiseMulti-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].text

Router 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

GuardrailMeasures
Input ValidationValidate all tool inputs, Sanitize user-provided data, Reject malformed requests
Execution LimitsMax iterations per task, Timeout per operation, Resource usage caps
Action ReviewHuman-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

See Also

Based on Anthropic Academy courses