AI Agents Workflow: Build & Deploy with Docker Compose

AI Agents Workflow: Building, Containerizing, and Deploying Automated Pipelines

Disclosure: This post contains one or more links to providers we have a real, registered affiliate/referral relationship with. We may earn a commission at no extra cost to you if you sign up through them.

If you’ve spent any time building with LLMs, you’ve noticed that a single prompt-response call rarely solves a real problem. Real automation requires an AI agents workflow: a chain of reasoning steps, tool calls, memory lookups, and error handling that runs reliably, unattended, on a schedule or in response to events. This guide walks through designing that workflow, containerizing it so it runs the same way on your laptop and in production, and deploying it on infrastructure that won’t fall over the first time an API call times out.

We’ll skip the theory-heavy intros you get elsewhere and go straight into architecture, code, and deployment mechanics that a developer or sysadmin can actually use today.

Why AI Agents Workflow Design Matters for DevOps Teams

Most teams start with a Jupyter notebook calling an LLM API. That’s fine for a prototype, but it breaks down fast once you need:

  • Retries and backoff when a model provider rate-limits you
  • Persistent state between agent steps (so a crash doesn’t lose progress)
  • Isolation between agents that shouldn’t share credentials or memory
  • Reproducible environments across dev, staging, and production
  • Observability into which step failed and why
  • This is exactly the kind of problem DevOps tooling was built to solve. Treating an AI agent like any other long-running service — something you containerize, schedule, log, and monitor — removes 90% of the flakiness people associate with “AI in production.”

    What Is an AI Agents Workflow, Really?

    Strip away the marketing language and an AI agents workflow is a directed graph of steps:

    1. Trigger — a cron job, webhook, queue message, or file event kicks things off.
    2. Planning — the agent (often an LLM call) decides which tool or sub-agent to invoke next.
    3. Tool execution — actual code runs: a database query, an API call, a shell command, a scrape.
    4. State update — results get written to memory (a vector store, Redis, or plain Postgres row).
    5. Loop or exit — the agent either continues to the next step or returns a final result.

    Frameworks like LangChain and LlamaIndex give you building blocks for steps 2–4, but the trigger, deployment, and reliability layer is still on you. That’s the part this article focuses on.

    Core Components of a Production AI Agents Workflow

    A workflow that survives contact with production needs five pieces, regardless of which agent framework you pick:

  • Orchestrator — decides step order (LangGraph, a custom state machine, or even a simple Python loop)
  • Model client — wraps calls to OpenAI, Anthropic, or a self-hosted model, with retry logic
  • Tool layer — sandboxed functions the agent can call (search, code execution, file I/O)
  • Persistence layer — Redis, Postgres, or SQLite for state and conversation history
  • Runtime — the container and scheduler that actually executes the workflow
  • We’ll build a minimal but real version of each.

    Designing Your First AI Agents Workflow

    Here’s a stripped-down agent loop in Python. It’s intentionally simple so you can see the control flow without framework magic:

    # agent.py
    import os
    import time
    import logging
    from openai import OpenAI
    
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger("agent")
    
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
    
    def call_model(messages, retries=3):
        for attempt in range(retries):
            try:
                response = client.chat.completions.create(
                    model="gpt-4o-mini",
                    messages=messages,
                    timeout=30,
                )
                return response.choices[0].message.content
            except Exception as e:
                wait = 2 ** attempt
                logger.warning(f"Model call failed ({e}), retrying in {wait}s")
                time.sleep(wait)
        raise RuntimeError("Model call failed after retries")
    
    def run_workflow(task: str) -> str:
        messages = [
            {"role": "system", "content": "You are an ops assistant. Be concise."},
            {"role": "user", "content": task},
        ]
        result = call_model(messages)
        logger.info("Workflow step complete")
        return result
    
    if __name__ == "__main__":
        output = run_workflow("Summarize last night's deployment logs.")
        print(output)

    This is deliberately boring: explicit retries, explicit logging, no hidden control flow. When you move to a framework like LangGraph for multi-step agents, keep that same discipline — every node should log its inputs and outputs so failures are traceable.

    Containerizing the Workflow with Docker

    Once the logic works locally, wrap it in a container so it behaves identically everywhere. Reference Docker’s official documentation if you’re new to multi-stage builds.

    # Dockerfile
    FROM python:3.12-slim AS base
    
    WORKDIR /app
    
    COPY requirements.txt .
    RUN pip install --no-cache-dir -r requirements.txt
    
    COPY agent.py .
    
    ENV PYTHONUNBUFFERED=1
    
    CMD ["python", "agent.py"]

    Keep the image slim — python:3.12-slim instead of the full image shaves hundreds of megabytes and reduces attack surface, which matters if this container is talking to external APIs with real credentials.

    Orchestrating Multiple Agents with Docker Compose

    Real workflows usually involve more than one agent plus supporting services (Redis for state, a Postgres database for logs). Docker Compose keeps this manageable without needing a full Kubernetes cluster for a side project or small team deployment.

    # docker-compose.yml
    version: "3.9"
    
    services:
      planner-agent:
        build: ./planner
        environment:
          - OPENAI_API_KEY=${OPENAI_API_KEY}
          - REDIS_URL=redis://redis:6379
        depends_on:
          - redis
        restart: unless-stopped
    
      worker-agent:
        build: ./worker
        environment:
          - REDIS_URL=redis://redis:6379
        depends_on:
          - redis
        restart: unless-stopped
        deploy:
          replicas: 2
    
      redis:
        image: redis:7-alpine
        volumes:
          - redis-data:/data
    
    volumes:
      redis-data:

    The planner-agent decides what work needs doing and pushes tasks onto a Redis queue; worker-agent instances pull tasks and execute tool calls. Running two worker replicas means one slow or stuck agent doesn’t block the whole pipeline — the same pattern you’d use for any queue-based worker system, covered in more depth in our Docker Compose production guide.

    Deploying to Production Infrastructure

    A container that runs locally still needs a home. For most AI agent workloads — which are bursty, I/O-bound on API calls, and don’t need GPU access if you’re calling a hosted model — a modest VPS is enough. A DigitalOcean droplet with 2 vCPUs and 4GB RAM comfortably runs several agent containers plus Redis and Postgres for a small-to-medium workload.

    If you’re running cost-sensitive batch jobs (nightly scraping agents, report generators), Hetzner offers some of the best price-per-core in the market and pairs well with Docker Swarm or Compose deployments that don’t need constant uptime guarantees.

    Deployment steps once you’ve provisioned a host:

    # on the server
    git clone https://github.com/yourorg/agent-workflow.git
    cd agent-workflow
    
    # set secrets
    cp .env.example .env
    nano .env  # add OPENAI_API_KEY, etc.
    
    # bring the stack up
    docker compose up -d --build
    
    # confirm everything is healthy
    docker compose ps
    docker compose logs -f planner-agent

    For anything public-facing (a webhook endpoint that triggers the workflow), put Cloudflare in front of it. Cloudflare’s proxy and WAF rules stop scrapers and abusive traffic from hammering your agent’s trigger endpoint, which matters because every unwanted request is a real API call you’re paying for downstream.

    Monitoring and Observability for AI Agent Pipelines

    Agent workflows fail in unusual ways compared to typical web services — a model might return malformed JSON, hallucinate a tool call that doesn’t exist, or silently loop. Standard uptime monitoring isn’t enough; you need to know when the logic is broken, not just when the container is down.

    A practical monitoring stack:

  • Container/host uptime — a service like BetterStack for status pages and uptime checks on your webhook trigger endpoint
  • Structured logs — ship container logs (docker compose logs) to a central place instead of leaving them on the host
  • Custom metrics — track agent-specific numbers: tool-call failure rate, average steps per task, token spend per run
  • Alerting thresholds — alert if failure rate crosses a threshold (say, more than 10% of runs erroring in a 15-minute window), not just on total outage
  • If you already run Prometheus for other services, exposing a /metrics endpoint from your agent container and scraping it is straightforward — we cover the general pattern in our self-hosted monitoring stack guide.

    Common Pitfalls When Running AI Agents in Production

    A few mistakes show up repeatedly in agent deployments:

  • No timeout on model calls — a hung API call blocks a worker indefinitely; always set an explicit timeout.
  • Unbounded retry loops — retries without a cap can rack up API costs fast when a provider is degraded.
  • Shared credentials across agents — give each agent its own scoped API key so you can revoke and audit independently.
  • No idempotency — if a task gets retried, make sure re-running it doesn’t duplicate side effects (double-sending an email, double-charging a card).
  • Ignoring cost per run — log token usage per task from day one; costs creep up unnoticed otherwise.
  • Running as root in the container — set a non-root user in your Dockerfile; agents that execute arbitrary tool code are a bigger risk surface than typical web apps.
  • Address these before scaling from a single agent to dozens — the failure modes compound quickly once you have more moving parts.

    Recommended: Ready to put this into practice? DigitalOcean is a tool we use for exactly this, and we have a real, disclosed affiliate relationship with them.

    FAQ

    Q: Do I need Kubernetes to run an AI agents workflow in production?
    A: No. Docker Compose on a single well-sized VPS handles most workloads fine. Move to Kubernetes only once you need multi-node scaling or complex autoscaling policies that Compose can’t express.

    Q: How do I handle secrets like API keys in a containerized agent workflow?
    A: Use environment variables injected at runtime via a .env file excluded from version control, or a secrets manager if you’re on a cloud provider. Never bake API keys into the Docker image itself.

    Q: What’s the difference between an agent workflow and a simple script with an LLM call?
    A: A script makes one call and returns. A workflow involves multiple steps, decision points, persistent state, and the ability to recover from partial failure — it behaves like a small service, not a one-off function.

    Q: Should each agent run in its own container?
    A: Generally yes, if agents have different dependencies, scaling needs, or security boundaries. It also makes debugging easier since logs and resource usage are isolated per container.

    Q: How do I keep API costs predictable with autonomous agents?
    A: Cap the maximum number of steps per task, log token usage per run, and set a hard budget alert. Runaway loops are the most common cause of unexpected bills.

    Q: Can I run the LLM itself locally instead of calling a hosted API?
    A: Yes — tools like Ollama let you run open models locally, removing per-call API costs, though you’ll need more CPU/RAM (or a GPU) on your host and should benchmark latency against your workflow’s needs before committing.

    An AI agents workflow is only as reliable as the infrastructure running it. Get the container, orchestration, and monitoring basics right first — the agent logic itself is the easy part once the plumbing doesn’t leak.

    Comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *