AI Engineering·How-To

Expose Your AI Agent via FastAPI

How to turn a Python LangGraph agent script into a production REST API with FastAPI: the minimum code, request models and how to run it.


FastAPI is incredibly fast, easy to learn, and auto-generates documentation (Swagger UI). Here is the absolute minimum boilerplate you need to expose your LLM agent.

The Code

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
# Assume we have an agent object from LangGraph or LangChain
from my_agent_logic import agent_graph

# 1. Initialize FastAPI
app = FastAPI(title="My AI Agent API")

# 2. Define the Request format
class ChatRequest(BaseModel):
    user_message: str
    session_id: str

# 3. Define the Response format
class ChatResponse(BaseModel):
    ai_response: str
    tokens_used: int

# 4. Create the Endpoint
@app.post("/api/chat", response_model=ChatResponse)
async def chat_endpoint(request: ChatRequest):
    try:
        # Pass the input to your agent
        # (This syntax depends on your specific agent framework)
        result = await agent_graph.ainvoke(
            {"messages": [("user", request.user_message)]},
            config={"configurable": {"thread_id": request.session_id}}
        )

        # Extract the final message content
        final_text = result["messages"][-1].content

        return ChatResponse(
            ai_response=final_text,
            tokens_used=0 # You can extract token metadata if needed
        )
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

How to Run It

Save the code above as main.py. Then run the Uvicorn server:

uvicorn main:app --reload

Navigate to http://localhost:8000/docs in your browser. You will see an interactive Swagger UI where you can immediately test your endpoint!

Chat with us