This is a very small LangChain v1 example that streams Anthropic Claude Haiku 4.5 output through FastAPI.
.
├── client.py
├── main.py
└── src
├── server.py
└── settings.py
src/settings.pyreadsANTHROPIC_API_KEYwith Pydantic settings.src/server.pyexposesPOST /chat/stream.- The endpoint builds a LangChain v1 agent with
create_agent(...). - It streams agent output with
astream(..., stream_mode="messages", version="v2"). - Each streamed text chunk is forwarded with FastAPI
StreamingResponse. client.pyis an optional local client if you want something simpler thancurl.
uv syncSet your Anthropic key before starting the app:
export ANTHROPIC_API_KEY="your-api-key"Optional: override the FastAPI port.
export PORT=8001Optional: override the default model alias.
export ANTHROPIC_MODEL="claude-haiku-4-5"uv run main.pyOpen the docs at http://127.0.0.1:${PORT:-8000}/docs.
uv run client.py "Explain streaming in 3 short sentences."If you omit the prompt, client.py uses a default one:
uv run client.py