Claude CodeAgentic AIAutomationWorkflow

How I Build Agentic AI Workflows with Claude Code (Step-by-Step)

Tariq OsmaniTariq Osmani8 min read
How I Build Agentic AI Workflows with Claude Code (Step-by-Step)

Most AI demos online are toys — single prompts that give you a clever answer and disappear. They have no memory, no tools, no real decision-making. That's not what I ship at Smart AI Workspace. The systems I build for clients are agentic: they think in steps, call real tools, remember context between calls, and make decisions based on what they discover.

When clients ask how I make the AI "think like a human," the answer is structure, not magic. You give it tools, state, and clear rules about which decisions it's allowed to make. This guide walks through exactly how I use Claude Code to build production-ready agentic AI automation workflows — the stack, the step-by-step process, and the mistakes I've learned the hard way.

What Makes a Workflow "Agentic"

An agentic workflow isn't just about getting answers — it's about building systems that can act. Three traits separate a real agent from a prompt:

  • Tool use. The system needs access to real tools — web scraping, database queries, email sending, API calls. Without tools, the AI can't actually do anything in the world.
  • Multi-step reasoning. The workflow plans sequences of actions: scrape leads, qualify them, route them to the right sales sequence. Each step depends on the previous one's results.
  • Conditional decision-making. The agent follows clear rules about when to branch. An agent that scrapes a lead list, qualifies each company by size, and routes them to different email sequences is making real decisions on data — not replaying a script.
SystemTool useMulti-stepMakes decisionsBest for
ChatbotNoNoNoAnswering questions
Standard workflowYesFixed orderNoPredictable, repeated tasks
Agentic workflowYesYesYesAmbiguous, judgment-heavy tasks

Think of it like a junior employee you can hand a research task to: it reads, decides, acts, and reports back — within boundaries you defined in advance.

The Core Loop Every Agent Runs

Under the hood, every agentic system runs the same loop — Claude Code included:

PERCEIVE → REASON → ACT → OBSERVE → REPEAT
  1. Perceive. Read the current state (inbox, database, web page).
  2. Reason. Decide the next step given the goal and what was just learned.
  3. Act. Call a tool — send an email, run a query, execute code.
  4. Observe. Read the result and update context.
  5. Repeat. Continue until the goal is reached or the budget is spent.

For a deeper look at how agentic systems decide and act — and where they still fail — see the AI agents guide.

My Stack for Agentic Automation

I use a specific stack that balances power with control:

  • Claude API — the reasoning engine. It understands context, plans multi-step work, and knows when to use tools.
  • n8n — orchestration. Visual workflow building with native support for Claude's tool-use patterns. (Why n8n over Zapier or Make: n8n vs Zapier vs Make.)
  • FastAPI — custom endpoints for integrations that don't fit n8n's visual paradigm.
  • PostgreSQL — state and memory so the agent keeps context across sessions.
  • Redis + Celery — async task queuing and workers for long-running operations.
  • Docker — consistent deployment across environments.

That stack gives production-grade reliability while keeping the development experience smooth.

Claude Code running an agentic automation workflow in the terminal

Step-by-Step: How I Build an Agentic Workflow with Claude Code

Here's the exact process I follow for every agentic automation system.

1. Define the task and decision points

I start by asking: what does the agent need to decide? What are the key branches in the logic? For the invoice automation project, the agent needed to decide: verify receipt, extract amounts, check against policy, route for approval. Every decision point gets explicit rules before any code is written.

2. Design the tool schema

Tools aren't a list of capabilities — they're precise JSON schemas. Here's a fragment from the invoice automation tool set:

{
  "functions": [
    {
      "name": "ocr_receipt",
      "description": "Extract text and amounts from receipt image",
      "parameters": {
        "type": "object",
        "properties": {
          "image_url": {"type": "string"},
          "vendor": {"type": "string"}
        }
      }
    }
  ]
}

Each tool has exact parameters, error handling, and retry logic built in.

3. Write the system prompt

The system prompt explains the agent's role, available tools, and decision rules. For the RAG research assistant, it includes: "You are an AI research assistant. You can search the web, analyze documents, and answer questions. Always cite your sources. If uncertain, say so and ask for clarification." It also includes tool descriptions, examples of good versus bad tool usage, and decision criteria.

4. Wire the tools in FastAPI or n8n

I connect Claude's tool use to real implementations. FastAPI endpoints handle Claude's function calls, validate inputs, and return structured responses. n8n nodes connect to Claude's tool execution engine, keeping the workflow visual. For invoice automation, a FastAPI endpoint receives Claude's tool calls, validates the accounting platform's OAuth token, and executes the business logic with proper error handling.

5. Add memory and state with PostgreSQL

State management prevents context loss. I store conversation history, agent decisions, and tool-call results so the agent remembers the current session, previous outputs, and its decision history. A lead-qualification agent that forgets prior interactions will re-contact the same company and lose credibility.

6. Test edge cases and failure modes

I test the happy path and every failure scenario: Claude failing to parse a document, tools timing out, networks dropping, rate limits. Each edge case gets fallback logic. For invoice automation, I test what happens when OCR fails, when a vendor isn't found in the accounting system, and when a policy violation occurs.

7. Deploy with Docker and monitor

I containerize everything with Docker, then set up monitoring and alerting. The system tracks Claude API usage (for cost control), tool-call success rates, and business metrics like invoices processed or leads qualified. You can't trust an agent you can't observe.

The same process works for everything from lead qualification to document processing. The key is treating the agent like a junior employee you train — clear rules, tools, and a process for handling uncertainty.

Common Mistakes When Building Agentic Workflows

From building these systems for clients, three failure patterns come up repeatedly:

  • Undefined tool boundaries. Give Claude too many tools, or tools without clear limits, and it gets confused, loops, or refuses to finish. Define exactly what each tool can do and when it should be used.
  • Missing state management. Without memory across steps, the agent loses context, duplicates work, and erodes trust.
  • Testing only the happy path. Real systems hit edge cases and unexpected inputs. Test every failure scenario and build recovery logic before deployment.

These three mistakes turn promising projects into expensive failures. Planning for the full lifecycle from day one avoids them.

What Agentic Workflows Return in Production

The 2026 data makes the case for building this way. Gartner projects 40% of enterprise applications will ship task-specific AI agents by 2026, up from less than 5% in 2025. KPMG's Q1 2026 AI Pulse Survey puts active agent deployment across core operations at 54% of organizations, up from 11% two years ago. On the return side, organizations deploying agentic systems report an average ROI of 171% (192% for US-based companies) — roughly 3x traditional automation returns.

The pattern: agents earn their cost when the task is narrow and repeatable but requires enough judgment that a hard-coded workflow breaks on edge cases. (More on the production realities: AI deployment at scale.)

How Smart AI Workspace Approaches This

I'm a solo founder, so every agent I ship needs to earn its keep quickly — there's no team to babysit it. That means the first conversation with a client is almost always about narrowing scope: pick the one workflow where agent intelligence clearly beats a workflow, define the tools and decision rules, ship something measurable, then generalize. Most agent projects die from ambition, not technology.

Build Your Own, or Let Me Build It For You

If you're ready to build an agentic workflow, the seven steps above are the pattern I use for everything from invoice processing to lead qualification to customer support automation. If you'd rather get the value without learning the stack, that's exactly the work I do — I'll build the system on infrastructure you own and hand you the keys.

See our services → · Talk to me about your project → · Hire me on Upwork → · Back to the homepage →


Sources: Gartner — 40% of Enterprise Apps Will Feature Task-Specific AI Agents by 2026 · KPMG Q1 2026 AI Pulse Survey via Joget · Agentic AI Stats 2026 — OneReach.ai · AI Agent Statistics — Datagrid · Claude Code Overview — Anthropic