- Building a minimal FastAPI app with a Upsonic agent using async endpoints and
agent.do_async() - Containerizing with Docker
- Running locally
Why async with FastAPI
FastAPI is async-native. Useagent.do_async() in your route handlers so the event loop is not blocked during LLM and tool calls. That keeps the server responsive under concurrent requests.
Setup
1
Create a new directory for your project
Create a new directory and navigate into it:Resulting structure:
2
Initialize the project with uv
Step 1: Create the FastAPI app
1
Create `main.py` with async endpoints and `agent.do_async()`
main.py
2
Set ANTHROPIC_API_KEY
3
Run the app
Step 2: Docker
1
Create a `.dockerignore` file
.dockerignore
2
Create a `Dockerfile`
Dockerfile
3
Build and run the image
4
Access the app
Open
http://localhost:8000. Interactive API docs: http://localhost:8000/docs.Step 3: Structured responses (async)
Use Pydantic models andagent.do_async() with response_format for typed JSON responses.
1
Add structured response in `main.py`
main.py
2
Test the structured endpoint
Key takeaways
- Use async route handlers and
await agent.do_async(task)so FastAPI’s event loop stays non-blocking. - Use
response_format=YourPydanticModelwhen you need structured JSON. - Run in production with
uv run uvicorn main:app --host 0.0.0.0 --port 8000or via Docker as above.

