Installation
FastAPI integration is an optional installation. To install all required dependencies, run:Setting Up FastAPI Endpoints
You can expose your agencies and tools as API endpoints using therun_fastapi() function.
Example: Create an API endpoint for a single agency
run_fastapi parameters
run_fastapi parameters
| Param | Type | Default | Description |
|---|---|---|---|
host | string | "0.0.0.0" | Interface to bind the server. |
port | integer | 8000 | Port to serve FastAPI on. |
app_token_env | string | "APP_TOKEN" | Env var name for the bearer token; if missing, auth is disabled. |
return_app | boolean | false | Return the FastAPI app instead of running the server. |
cors_origins | list | ["*"] | Allowed CORS origins. |
enable_agui | boolean | false | Enable AG-UI streaming (hides the cancel endpoint). |
enable_logging | boolean | false | Track requests and expose /get_logs at the server root. |
logs_dir | string | "activity-logs" | Folder for request logs when logging is enabled. |
allowed_local_file_dirs | list | None | Allowlist for local file_urls; paths outside the list are rejected. |
Endpoint reference
Endpoint reference
- Agencies are served at:
/your_agency_name/get_response(POST)/your_agency_name/get_response_stream(POST, streaming responses)/your_agency_name/cancel_response_stream(POST; not registered whenenable_agui=True)/your_agency_name/get_metadata(GET)/get_logs(GET; whenenable_logging=True)
- Tools registered via
tools=[...]are available at/tool/ToolClassName(BaseTools) or/tool/function_name(function tools). - OpenAPI and interactive docs:
/openapi.json,/docs,/redoc.
Response format
Response format
Non-streaming (Streaming (Cancel (Metadata (Conversation starters appear under
/get_response):/get_response_stream):/cancel_response_stream):/get_metadata):data.conversationStarters on each agent node when configured. See Conversation starters cache.Tool (/tool/<name>):Usage tracking
Usage tracking
Responses include a If you’re using LiteLLM models and want usage in streaming responses, keep
usage object with token counts and cost by default. For streaming, the final event: messages payload includes the same usage object.To understand what’s inside usage, see Observability.Usage tracking is configured on the agent (not on FastAPI):include_usage=True.Stream cancellation uses an in-memory registry per process. Use single-worker deployments (e.g., uvicorn
workers=1) or sticky routing so cancel requests reach the same worker.Authentication
Set the environment variable named byapp_token_env (default APP_TOKEN) to require Authorization: Bearer <token> on every endpoint. When the variable is absent, authentication is disabled.
Implementation reference
Example: Serving Multiple Agencies and Tools
Example: Serving Multiple Agencies and Tools
example_tool and test_tool.API Usage Example
You can interact with your agents and tools using HTTP requests:Request payload
Request payload
| Field | Type | Required | Description |
|---|---|---|---|
message | string | Yes | User message to start or continue the conversation. |
chat_history | list | No | Flat list of prior messages with metadata (agent, callerAgent, timestamp) to preserve context. |
recipient_agent | string | No | Target agent when you want to direct the next turn. |
file_ids | list | No | IDs of already uploaded files to attach. |
file_urls | object | No | {filename: url_or_absolute_path} map. Local paths require allowed_local_file_dirs on run_fastapi. |
additional_instructions | string | No | Extra guidance for the current request. |
user_context | object | No | Structured data passed to Agency Context without exposing it to the LLM. |
client_config | object | No | Override base_url / api_key for this request (and optional litellm_keys for litellm/ models). |
How user_context is applied
- Merges with any
user_contextset on the agency instance. - Useful for structured data (ids, preferences, feature flags) you do not want in the prompt.
- Accessible within tools for the duration of the run. See Agency Context.
How client_config is applied
- Applies only to OpenAI models (no prefix or
openai/...) and LiteLLM models (litellm/...). - Custom Model subclasses are not modified; the request still runs, but the override is skipped.
- For LiteLLM, you can provide
litellm_keysto pass different keys per provider. Requires LiteLLM installed.
client_config fields
base_url(string, optional): Override the API base URL for this request.api_key(string, optional): Override the API key for this request.litellm_keys(object, optional): Only forlitellm/...models.- Map
provider_name→api_key. - Example:
{"anthropic": "...", "gemini": "..."}
- Map
client_config examples
client_config examples
OpenAI model override (gpt-4o):LiteLLM mixed providers (requires
openai-agents[litellm]):Cancelling Active Streams
The streaming endpoint supports cancellation via two methods:1. Automatic Cancellation on Disconnect
When a client disconnects (tab close, refresh, network failure), the stream is automatically cancelled to preserve token costs.2. Cancel Endpoint
Call the cancel endpoint with therun_id received from the first event of the streaming response.
This will allow you to retrieve intermediate results that were generated before the run cancellation.
Optionally include cancel_mode (defaults to immediate):
immediate— stop right away and return messages that were fully generated; the in-progress message is discarded.after_turn— finish the current turn, then stop.
Serving Standalone Tools
Expose tools as simple HTTP endpoints for external systems, webhooks, or other agents to call directly without agency orchestration.POST /tool/Address— execute the toolGET /openapi.json— full OpenAPI schemaGET /docs— interactive Swagger UI
Generate OpenAPI schema programmatically
Generate OpenAPI schema programmatically
Use
ToolFactory.get_openapi_schema() to generate the OpenAPI spec programmatically:File Attachments
Attach files to agency requests usingfile_ids or file_urls in the payload:
file_ids_map with the uploaded file IDs:
.pdf, .jpeg, .jpg, .gif, .png, .c, .cs, .cpp, .csv, .html, .java, .json, .php, .py, .rb, .css, .js, .sh, .ts, .pkl, .tar, .xlsx, .xml, .zip, .doc, .docx, .md, .pptx, .tex, .txt
Local file paths
Local file paths
To support passing local filepaths in the Then pass absolute file paths in Invalid paths return an
file_urls field, set allowed_local_file_dirs on run_fastapi:file_urls:error field in the response body.