agentic-ai-course

Module 14 — Deploying an Agent as a Service

Every module so far has been a CLI chat loop. A real product usually needs the agent reachable over HTTP instead — a frontend, a Slack bot, another backend service, all calling one endpoint. This module wraps the Module 2-style calculator agent in a minimal FastAPI server.

What you’ll learn

The core idea

SESSIONS: dict[str, list[dict]] = {}

@app.post("/chat", response_model=ChatResponse)
def chat(request: ChatRequest) -> ChatResponse:
    history = SESSIONS.setdefault(request.session_id, [])
    history.append({"role": "user", "content": request.message})
    reply = run_agent_turn(history)
    return ChatResponse(reply=reply)

The agent loop (run_agent_turn) is unchanged from Module 2 — the entire new idea is that SESSIONS keeps each session_id’s messages list alive across separate HTTP requests, the same role a global variable played in every CLI module’s while True: loop.

This in-memory dict is a demo shortcut, and it’s wrong for production in two specific ways worth naming: it resets to empty every time the process restarts, and it doesn’t work if you ever run more than one server process (each process has its own separate dict, so a user’s session might land on a different process on the next request and find nothing). A real deployment would put SESSIONS in Redis or a database — keyed the same way, just durable and shared — everything else in this module stays the same.

Files

Run it

pip install -r requirements.txt
export ANTHROPIC_API_KEY=your_key_here
uvicorn solution:app --reload --port 8000

Then in another terminal:

curl -X POST http://localhost:8000/chat \
  -H "Content-Type: application/json" \
  -d '{"session_id": "demo", "message": "What is 12 * 8?"}'

curl -X POST http://localhost:8000/chat \
  -H "Content-Type: application/json" \
  -d '{"session_id": "demo", "message": "Add 10 to that."}'

The second call should correctly reference the first answer — proof the session persisted.

This is the last module

You’ve now covered the full path from a single stateless API call to an agent that remembers, specializes, stays safe, delegates, streams, retrieves, gets tested, runs efficiently at scale, connects to standardized tool servers, manages its own growing context, reasons through hard problems, and ships as a real service. That’s the complete loop.