From Prompt to Production: Dockerizing a LangChain Agent with FastAPI
Chandrani Mukherjee

Chandrani Mukherjee @moni121189

About: As a Sr. Solution Enterprise Architect and MS in AI/ML from Liverpool John Moors University , UK, I have been a key contributor to global organizations like Mphasis AI, McKesson, First Abu Dhabi Bank

Location:
New Jersey
Joined:
Jul 5, 2025

From Prompt to Production: Dockerizing a LangChain Agent with FastAPI

Publish Date: Jul 6
46 4

Supercharge your AI app deployment with Docker, FastAPI, and LangChain in one seamless containerized pipeline.

🧠 Overview

As AI-powered apps become more complex, managing dependencies, serving endpoints, and ensuring smooth deployment are top priorities. In this post, you'll learn how to dockerize a LangChain agent wrapped with FastAPI — giving you a ready-to-deploy, production-friendly container for your intelligent applications.

By the end, you’ll:

  • Create a LangChain agent
  • Wrap it with FastAPI for a clean REST interface
  • Dockerize the entire setup
  • Run it anywhere with just one command

📦 Prerequisites

Before you begin, make sure you have:

  • Docker installed
  • Python 3.10+ (for local testing)
  • An OpenAI API Key or any LLM key supported by LangChain

📁 Project Structure

langchain-agent-api/
├── agent_app/
│ ├── main.py
│ └── agent.py
├── requirements.txt
├── Dockerfile
└── .env

pgsql

✨ Step 1: Create the LangChain Agent

agent_app/agent.py

from langchain.agents import initialize_agent, Tool
from langchain.llms import OpenAI
from langchain.utilities import SerpAPIWrapper
import os

def create_agent():
    llm = OpenAI(temperature=0, openai_api_key=os.getenv("OPENAI_API_KEY"))
    search = SerpAPIWrapper()
    tools = [Tool(name="Search", func=search.run, description="Useful for answering general questions.")]
    agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)
    return agent
Enter fullscreen mode Exit fullscreen mode

🚀 Step 2: Wrap with FastAPI
agent_app/main.py

from fastapi import FastAPI
from pydantic import BaseModel
from agent import create_agent

app = FastAPI()
agent = create_agent()

class Query(BaseModel):
    question: str

@app.post("/ask")
async def ask_question(query: Query):
    response = agent.run(query.question)
    return {"response": response}
Enter fullscreen mode Exit fullscreen mode

📄 Step 3: Define Requirements
requirements.txt

fastapi
uvicorn
langchain
openai
python-dotenv
Enter fullscreen mode Exit fullscreen mode

💡 Add serpapi or other tools as needed.

🛠️ Step 4: Dockerfile
Dockerfile

FROM python:3.10-slim

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY agent_app ./agent_app
COPY .env .

CMD ["uvicorn", "agent_app.main:app", "--host", "0.0.0.0", "--port", "8000"]
Enter fullscreen mode Exit fullscreen mode

🔑 Step 5: Add Environment Variables
.env

OPENAI_API_KEY=your_openai_key_here
SERPAPI_API_KEY=your_serpapi_key_here

⚠️ Never commit .env to public repos. Use Docker secrets or CI/CD env vars in production.

🧪 Step 6: Build and Run
🧱 Build the Docker image

docker build -t langchain-agent-api .
Enter fullscreen mode Exit fullscreen mode

🚀 Run the container

docker run --env-file .env -p 8000:8000 langchain-agent-api
Enter fullscreen mode Exit fullscreen mode

📬 Try It Out
Once running, test your agent with:

curl -X POST http://localhost:8000/ask \
-H "Content-Type: application/json" \
-d '{"question": "Who is the CEO of OpenAI?"}'
Enter fullscreen mode Exit fullscreen mode

Your containerized LangChain agent should reply in seconds! 🤖

📦 Bonus: Add Docker Compose (Optional)
docker-compose.yml


version: "3.8"
services:
  langchain:
    build: .
    ports:
      - "8000:8000"
    env_file:
      - .env
Enter fullscreen mode Exit fullscreen mode

Then run:


docker-compose up --build
Enter fullscreen mode Exit fullscreen mode

🏁 Final Thoughts
You now have a production-ready, containerized LangChain agent served via FastAPI. Whether you’re building internal AI tools or deploying to the cloud, this setup gives you repeatability, portability, and power.

Comments 4 total

  • di
    diJul 7, 2025

    Great article! I have just one note: the run method in agent.run was deprecated in LangChain version 0.1.0 and replaced with invoke. Also, both run and invoke are blocking methods, so since you’re using the async interface, you’ll need to call the agent with ainvoke

    • Chandrani Mukherjee
      Chandrani MukherjeeJul 8, 2025

      Hey Thanks for the appreciation and input. Indeed you are correct. Will surely make articles using asynchronous invoke

  • Aiden Benjamin
    Aiden BenjaminJul 23, 2025

    Dockerizing LLMs is still new for many devs — you made it so accessible.

  • Lucas Henry
    Lucas HenryJul 23, 2025

    Curious: how do you handle persistent memory storage for agents in Docker?

Add comment