|
| 1 | +# OpenAI Agents SDK |
| 2 | + |
| 3 | +<div class='subtitle'>Use Gateway with the Agents SDK</div> |
| 4 | + |
| 5 | +[OpenAI Agents SDK](https://openai.github.io/openai-agents-python/) relies on OpenAI's Python client, which makes it very easy to integrate Gateway, similar to the standard [OpenAI integration](../llm-provider-integrations/openai.md). |
| 6 | + |
| 7 | +## Getting the Invariant API Key |
| 8 | + |
| 9 | +Visit the [Explorer Documentation](https://explorer.invariantlabs.ai/docs/explorer) to learn how to obtain your own API key. |
| 10 | + |
| 11 | +## SDK Integration |
| 12 | + |
| 13 | +You can then use Gateway with the Agents SDK by re-configuring the OpenAI client's base URL: |
| 14 | + |
| 15 | +```python |
| 16 | +from agents import Agent, OpenAIChatCompletionsModel, Runner, function_tool |
| 17 | +from openai import AsyncOpenAI |
| 18 | +import os |
| 19 | + |
| 20 | +external_client = AsyncOpenAI( |
| 21 | + base_url="https://explorer.invariantlabs.ai/api/v1/gateway/weather-agent/openai", |
| 22 | + default_headers={ |
| 23 | + "Invariant-Authorization": "Bearer " + os.getenv("INVARIANT_API_KEY"), |
| 24 | + }, |
| 25 | +) |
| 26 | + |
| 27 | + |
| 28 | +@function_tool |
| 29 | +async def fetch_weather(location: str) -> str: |
| 30 | + return "sunny" |
| 31 | + |
| 32 | + |
| 33 | +agent = Agent( |
| 34 | + name="Assistant", |
| 35 | + instructions="You are a helpful assistant", |
| 36 | + model=OpenAIChatCompletionsModel( |
| 37 | + model="gpt-4o", |
| 38 | + openai_client=external_client, |
| 39 | + ), |
| 40 | + tools=[fetch_weather], |
| 41 | +) |
| 42 | + |
| 43 | +result = Runner.run_sync( |
| 44 | + agent, |
| 45 | + "What is the weather like in Paris?", |
| 46 | +) |
| 47 | +print(result.final_output) |
| 48 | + |
| 49 | +``` |
| 50 | + |
| 51 | +This will automatically trace your agent interactions in Invariant Explorer. |
| 52 | + |
| 53 | +## Explore Other Integrations |
| 54 | + |
| 55 | +<div class='tiles'> |
| 56 | + |
| 57 | +<a href="../microsoft-autogen" class='tile'> |
| 58 | + <span class='tile-title'>Microsoft AutoGen Integration →</span> |
| 59 | + <span class='tile-description'>Enhance and debug your Microsoft AutoGen agents effortlessly using the Gateway.</span> |
| 60 | +</a> |
| 61 | + |
| 62 | +<a href="../openhands" class='tile'> |
| 63 | + <span class='tile-title'>OpenHands Integration →</span> |
| 64 | + <span class='tile-description'>Streamline the development and debugging of OpenHands applications with the Gateway.</span> |
| 65 | +</a> |
| 66 | + |
| 67 | +<a href="../swe-agent" class='tile'> |
| 68 | + <span class='tile-title'>SWE-agent Integration →</span> |
| 69 | + <span class='tile-description'>Streamline the development and debugging of SWE-agent applications with the Gateway.</span> |
| 70 | +</a> |
| 71 | + |
| 72 | +<a href="../browser-use" class='tile'> |
| 73 | + <span class='tile-title'>Browser Use Integration →</span> |
| 74 | + <span class='tile-description'>Optimize and troubleshoot your Browser Use applications with Invariant Gateway.</span> |
| 75 | +</a> |
| 76 | + |
| 77 | +</div> |
0 commit comments