deployment-to-production.mdx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. ---
  2. title: "Deployment to Production"
  3. description: "Step-by-step guide for deploying your agency in a production environment."
  4. icon: "rocket-launch"
  5. sidebarTitle: "Deploy"
  6. ---
  7. **Recommended:** Use the [Starter Template](/welcome/getting-started/starter-template) for production. It ships with FastAPI endpoints, auth, and a clean project layout.
  8. ## Required Environment Variables
  9. Before deploying, ensure these are set in your production environment:
  10. | Variable | Required | Description |
  11. |----------|----------|-------------|
  12. | `OPENAI_API_KEY` | Yes | Your OpenAI API key |
  13. | `APP_TOKEN` | Recommended | Authentication token for FastAPI endpoints |
  14. <Note>
  15. Thread persistence uses callbacks you define to store threads in any database you choose.
  16. </Note>
  17. <Note>
  18. This guide assumes you have already created an agency. If you haven't, check out the [Getting Started](/welcome/installation) guide.
  19. </Note>
  20. <Warning>
  21. Before deploying, ensure you have thoroughly tested all tools and agents. Run the test cases in each tool file and verify the agency works end-to-end using demo methods.
  22. </Warning>
  23. ## Deployment Process
  24. <Steps>
  25. <Step title="Step 1: Persist Conversation Threads" icon="message-dots">
  26. By default, every time you create a new `Agency()`, it starts a fresh conversation thread. In production, you usually need to resume prior conversations or handle multiple users.
  27. <Info>
  28. Persist the full conversation history for each chat, including user-facing turns and agent-to-agent handoffs.
  29. </Info>
  30. Chat persistence is handled through callback functions passed to the Agency constructor:
  31. ```python
  32. from agents import TResponseInputItem
  33. from agency_swarm import Agency
  34. def save_threads(messages: list[TResponseInputItem], chat_id: str) -> None:
  35. save_threads_to_db(chat_id, messages)
  36. def load_threads(chat_id: str) -> list[TResponseInputItem]:
  37. return load_threads_from_db(chat_id)
  38. agency = Agency(
  39. agent1,
  40. agent2,
  41. communication_flows=[(agent1, agent2)],
  42. load_threads_callback=lambda: load_threads(chat_id),
  43. save_threads_callback=lambda messages: save_threads(messages, chat_id),
  44. )
  45. ```
  46. <Warning>
  47. If you switch model providers for an existing saved chat, old tool/event items may no longer replay correctly. Start a new chat, or keep only `{role, content}` messages.
  48. </Warning>
  49. </Step>
  50. <Step title="Step 2: Configure FastAPI Endpoints" icon="diagram-project">
  51. Use FastAPI in one of two ways:
  52. - Single agency: call `agency.run_fastapi(...)` from an `Agency` instance.
  53. - Multiple agencies and/or standalone tools: use top-level `run_fastapi(agencies=..., tools=[...])`.
  54. <Info>
  55. There can be multiple agencies in one server, and each agency key becomes its own endpoint prefix.
  56. </Info>
  57. ```python
  58. from agency_swarm import Agency, Agent, function_tool, run_fastapi
  59. @function_tool
  60. def health_check() -> str:
  61. return "ok"
  62. def create_support_agency(load_threads_callback=None):
  63. support = Agent(name="Support", instructions="You are a support agent.")
  64. return Agency(
  65. support,
  66. name="support",
  67. load_threads_callback=load_threads_callback,
  68. )
  69. def create_sales_agency(load_threads_callback=None):
  70. sales = Agent(name="Sales", instructions="You are a sales agent.")
  71. return Agency(
  72. sales,
  73. name="sales",
  74. load_threads_callback=load_threads_callback,
  75. )
  76. run_fastapi(
  77. agencies={
  78. "support": create_support_agency,
  79. "sales": create_sales_agency,
  80. },
  81. tools=[health_check],
  82. app_token_env="APP_TOKEN",
  83. cors_origins=["https://your-app.example"],
  84. )
  85. ```
  86. <Note>
  87. `run_fastapi(agencies=...)` injects `load_threads_callback` per request (for `chat_history`) and does not inject `save_threads_callback`.
  88. If you need server-side persistence writes, wire that explicitly in your application flow.
  89. </Note>
  90. This creates separate agency endpoints plus tool endpoints, for example:
  91. - `/support/get_response` and `/support/get_response_stream`
  92. - `/sales/get_response` and `/sales/get_response_stream`
  93. - `/tool/health_check`
  94. FastAPI details:
  95. - [Setting Up FastAPI Endpoints](/additional-features/fastapi-integration#setting-up-fastapi-endpoints)
  96. - [Authentication](/additional-features/fastapi-integration#authentication)
  97. - [Implementation reference (multiple agencies and tools)](/additional-features/fastapi-integration#implementation-reference)
  98. - [API Usage Example](/additional-features/fastapi-integration#api-usage-example)
  99. If you need tools hosted separately from your agency service, expose tools as APIs and connect them with [OpenAPI schemas](/core-framework/tools/openapi-schemas), or use [MCP Integration](/core-framework/tools/mcp-integration).
  100. </Step>
  101. <Step title="Step 3: Deploy the Service" icon="rocket-launch">
  102. Use the [Starter Template](/welcome/getting-started/starter-template) as your production base. It already includes FastAPI wiring and deployment defaults.
  103. - Create a repo from the template
  104. - Set `OPENAI_API_KEY` and `APP_TOKEN`
  105. - Follow the template README to deploy
  106. If you are wiring your own server, see [FastAPI Integration](/additional-features/fastapi-integration) for endpoint and parameter details (`host`, `port`, `app_token_env`, `cors_origins`, `enable_agui`).
  107. </Step>
  108. </Steps>