third-party-models.mdx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. ---
  2. title: "Third-Party Models"
  3. description: "Use models from Anthropic, Google, AWS, or self-hosted open-source models via LiteLLM."
  4. icon: "server"
  5. ---
  6. While OpenAI is generally recommended, there are situations where you might prefer third-party models. Agency Swarm supports proprietary providers (Anthropic, Google, AWS) and self-hosted open-source models (Llama, Mistral, etc.) through LiteLLM integration:
  7. ## LiteLLM Integration
  8. Since Agents SDK no longer uses assistants, most of the previously available frameworks became incompatible with it. One of the few frameworks that has been ported for the new SDK is [LiteLLM](https://docs.litellm.ai/docs/response_api), which you can use to connect your agent to various providers (Anthropic, Google Vertex AI, AWS Bedrock, Azure) as well as self-hosted open-source models via Ollama, vLLM, and other local serving solutions.
  9. <Tabs>
  10. <Tab title="Using OpenAI's LiteLLM model">
  11. <Steps>
  12. <Step title="Install LiteLLM">
  13. Install LiteLLM to get started with open-source model support:
  14. ```bash
  15. pip install "openai-agents[litellm]"
  16. ```
  17. </Step>
  18. <Step title="Configure Agency Swarm Agent">
  19. Create an agent that connects to your LiteLLM proxy:
  20. ```python
  21. import os
  22. from agency_swarm import Agent
  23. from agents.extensions.models.litellm_model import LitellmModel
  24. # Requires GOOGLE_API_KEY environment variable set
  25. gemini_agent = Agent(
  26. name="GeminiAgent",
  27. instructions="You are a helpful assistant",
  28. model="litellm/gemini/gemini-2.0-flash"
  29. )
  30. ```
  31. </Step>
  32. <Step title="Create and Run Agency">
  33. Set up your agency and start using third-party models:
  34. ```python
  35. from agency_swarm import Agency
  36. agency = Agency(gemini_agent)
  37. agency.tui()
  38. ```
  39. The first terminal run downloads the matching terminal app automatically.
  40. </Step>
  41. </Steps>
  42. </Tab>
  43. <Tab title="Using proxy server">
  44. <Steps>
  45. <Step title="Install LiteLLM">
  46. Install LiteLLM to get started with open-source model support:
  47. ```bash
  48. pip install "litellm[proxy]"
  49. ```
  50. </Step>
  51. <Step title="Create LiteLLM Configuration">
  52. Create a `config.yaml` file to configure your models and providers:
  53. ```yaml
  54. model_list:
  55. - model_name: gemini-flash
  56. litellm_params:
  57. model: gemini/gemini-2.0-flash
  58. api_key: os.environ/GEMINI_API_KEY # or paste your key directly here
  59. - model_name: claude-sonnet
  60. litellm_params:
  61. model: anthropic/claude-3-5-sonnet-20240620
  62. api_key: os.environ/ANTHROPIC_API_KEY
  63. - model_name: llama-groq
  64. litellm_params:
  65. model: groq/llama-3.1-70b-versatile
  66. api_key: os.environ/GROQ_API_KEY
  67. general_settings:
  68. store_prompts_in_spend_logs: true # Enable session management
  69. ```
  70. </Step>
  71. <Step title="Set Environment Variables">
  72. Add your API keys to your environment variables:
  73. ```bash
  74. export GEMINI_API_KEY="your-gemini-api-key"
  75. export ANTHROPIC_API_KEY="your-anthropic-api-key"
  76. export GROQ_API_KEY="your-groq-api-key"
  77. ```
  78. </Step>
  79. <Step title="Start LiteLLM Proxy Server">
  80. Launch the LiteLLM proxy server with your configuration:
  81. ```bash
  82. litellm --config /path/to/config.yaml
  83. # Server will start on http://localhost:4000
  84. ```
  85. </Step>
  86. <Step title="Configure Agency Swarm Agent">
  87. Create an agent that connects to your LiteLLM proxy:
  88. ```python
  89. import os
  90. from openai import AsyncOpenAI
  91. from agency_swarm import Agent, OpenAIChatCompletionsModel
  92. custom_client = AsyncOpenAI(
  93. api_key="xxx", # Any if proxy key wasn't set
  94. base_url="http://localhost:4000",
  95. )
  96. gemini_agent = Agent(
  97. name="GeminiAgent",
  98. instructions="You are a helpful assistant",
  99. model=OpenAIChatCompletionsModel(
  100. model="gemini/gemini-2.0-flash",
  101. openai_client=custom_client
  102. )
  103. )
  104. ```
  105. </Step>
  106. <Step title="Create and Run Agency">
  107. Set up your agency and start using third-party models:
  108. ```python
  109. from agency_swarm import Agency
  110. agency = Agency(gemini_agent)
  111. agency.tui()
  112. ```
  113. The first terminal run downloads the matching terminal app automatically.
  114. </Step>
  115. </Steps>
  116. </Tab>
  117. </Tabs>
  118. ## Using model-specific tools
  119. Some models, like gemini or claude have their internal tools, which can be attached to an agent by utilizing `extra_body` parameter in agent's `model_settings`:
  120. ```python
  121. import os
  122. from agency_swarm import Agent
  123. from agents.extensions.models.litellm_model import LitellmModel
  124. # Requires GOOGLE_API_KEY environment variable set
  125. gemini_agent = Agent(
  126. name="GeminiAgent",
  127. instructions="You are a helpful assistant",
  128. model="litellm/gemini/gemini-2.0-flash"
  129. )
  130. # Requires XAI_API_KEY environment variable set
  131. grok_agent = Agent(
  132. name="GrokAgent",
  133. instructions="You are a helpful assistant",
  134. model="litellm/xai/grok-4-0709"
  135. )
  136. ```
  137. Here both Grok and Gemini agents will be able to use their native search tools, which are similar to OpenAI's WebSearch() tool. Consider checking out [LiteLLM's documentation](https://docs.litellm.ai/docs) to find a full list of supported tools.
  138. ## Limitations
  139. <Warning>
  140. Be aware of the limitations when using third-party models.
  141. </Warning>
  142. - **Hosted tools are not supported**: Patched agents are not able to utilize hosted tools, such as WebSearch, FileSearch, CodeInterpreter and others.
  143. - **Patched and unpatched models should not use handoffs to communicate**: You may use standard OpenAI client and patched agents in a single agency, however using handoff to transfer chat from patched model to unpatched or vice-versa will lead to an error.
  144. - **Function calling may not be supported by some third-party models**: This limitation prevents the agent from communicating with other agents in the agency. Therefore, it must be positioned at the end of the agency chart and cannot utilize any tools.
  145. - **RAG is typically limited**: Most open-source implementations have restricted Retrieval-Augmented Generation capabilities. It is recommended to develop a custom tool with your own vector database.
  146. - **Potential library conflicts**: the Agents SDK is still a fairly new framework which is being actively developed and improved. Due to that, there might be potential conflicts between litellm and openai-agents packages on recent releases.
  147. For Azure OpenAI, see [Azure OpenAI](/additional-features/azure-openai).
  148. ## Future Plans
  149. Updates will be provided as new open-source assistant API implementations stabilize.
  150. If you successfully integrate other projects with agency-swarm, please share your experience through an issue or pull request.