Skip to content

Commit e184439

Browse files
committed
Adding openai agents config
1 parent fdb0d16 commit e184439

File tree

2 files changed

+191
-0
lines changed

2 files changed

+191
-0
lines changed

CLAUDE.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,81 @@ The package provides the `agentex` CLI with these main commands:
4040
- Debug agents: `agentex agents run --manifest manifest.yaml --debug-worker`
4141
- Debug with custom port: `agentex agents run --manifest manifest.yaml --debug-worker --debug-port 5679`
4242

43+
### Custom OpenAI Client for Agents SDK
44+
45+
Configure custom OpenAI clients **specifically for the OpenAI Agents SDK** integration (Agent and Runner classes).
46+
47+
⚠️ **Scope**: This configuration ONLY affects OpenAI Agents SDK operations. It does NOT affect:
48+
- LiteLLM integration (configure LiteLLM separately via environment variables or LiteLLM config)
49+
- SGP integration
50+
- Direct OpenAI API calls
51+
52+
#### Requirements
53+
- Must use **async** client: `AsyncOpenAI` or `AsyncAzureOpenAI`
54+
- Sync clients (`OpenAI`) are not supported by the Agents SDK
55+
56+
#### Basic Usage (Custom Endpoint)
57+
58+
Use this for custom OpenAI-compatible endpoints such as LiteLLM proxy for cost tracking:
59+
60+
```python
61+
from openai import AsyncOpenAI
62+
from agentex.lib.adk.providers._modules.openai_agents_config import (
63+
initialize_openai_agents_client
64+
)
65+
66+
# Configure custom endpoint
67+
client = AsyncOpenAI(
68+
base_url="https://your-proxy.com/v1",
69+
api_key=os.getenv("CUSTOM_API_KEY")
70+
)
71+
initialize_openai_agents_client(client)
72+
```
73+
74+
#### Azure OpenAI
75+
76+
```python
77+
from openai import AsyncAzureOpenAI
78+
from agentex.lib.adk.providers._modules.openai_agents_config import (
79+
initialize_openai_agents_client
80+
)
81+
82+
client = AsyncAzureOpenAI(
83+
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
84+
api_key=os.getenv("AZURE_OPENAI_KEY"),
85+
api_version="2024-02-01"
86+
)
87+
initialize_openai_agents_client(client)
88+
```
89+
90+
#### Temporal Workers
91+
92+
Call `initialize_openai_agents_client()` in your worker startup script **BEFORE** starting the worker:
93+
94+
```python
95+
# run_worker.py
96+
import os
97+
from openai import AsyncOpenAI
98+
from agentex.lib.adk.providers._modules.openai_agents_config import (
99+
initialize_openai_agents_client
100+
)
101+
102+
# Step 1: Configure client before starting worker
103+
if os.getenv("CUSTOM_OPENAI_BASE_URL"):
104+
client = AsyncOpenAI(
105+
base_url=os.getenv("CUSTOM_OPENAI_BASE_URL"),
106+
api_key=os.getenv("OPENAI_API_KEY")
107+
)
108+
initialize_openai_agents_client(client)
109+
110+
# Step 2: Start worker (all agent operations will use configured client)
111+
# ... worker startup code ...
112+
```
113+
114+
#### Backward Compatibility
115+
116+
If `initialize_openai_agents_client()` is not called, the OpenAI Agents SDK uses default OpenAI configuration via the `OPENAI_API_KEY` environment variable. All existing code continues to work without changes.
117+
43118
## Architecture Overview
44119

45120
### Code Structure
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
"""
2+
OpenAI Agents SDK Client Configuration
3+
4+
Configures custom OpenAI clients specifically for the OpenAI Agents SDK.
5+
This affects ONLY the OpenAI Agents SDK integration (Agent and Runner).
6+
7+
For LiteLLM integration, use separate LiteLLM configuration methods.
8+
"""
9+
10+
from typing import Union
11+
from openai import AsyncOpenAI, AsyncAzureOpenAI
12+
from agents import set_default_openai_client
13+
from agentex.lib.utils.logging import make_logger
14+
15+
logger = make_logger(__name__)
16+
17+
# Type alias for supported async OpenAI clients
18+
AsyncOpenAIClient = Union[AsyncOpenAI, AsyncAzureOpenAI]
19+
20+
_client_initialized = False
21+
22+
23+
def initialize_openai_agents_client(client: AsyncOpenAIClient) -> None:
24+
"""
25+
Initialize custom OpenAI client for OpenAI Agents SDK operations.
26+
27+
⚠️ IMPORTANT: This ONLY affects the OpenAI Agents SDK integration
28+
(Agent and Runner classes). It does NOT affect:
29+
- LiteLLM integration (use LiteLLM configuration separately)
30+
- SGP integration
31+
- Direct OpenAI API calls
32+
33+
This should be called ONCE at application or worker startup, before any
34+
agents are created using the OpenAI Agents SDK.
35+
36+
Args:
37+
client: Pre-configured async OpenAI client (AsyncOpenAI or AsyncAzureOpenAI)
38+
39+
Raises:
40+
TypeError: If a non-async client is passed (will be caught by type checker)
41+
42+
Examples:
43+
# Custom endpoint (e.g., LiteLLM proxy for cost tracking):
44+
from openai import AsyncOpenAI
45+
from agentex.lib.adk.providers._modules.openai_agents_config import (
46+
initialize_openai_agents_client
47+
)
48+
49+
client = AsyncOpenAI(
50+
base_url="https://your-proxy.com/v1",
51+
api_key=os.getenv("CUSTOM_API_KEY")
52+
)
53+
initialize_openai_agents_client(client)
54+
55+
# Azure OpenAI:
56+
from openai import AsyncAzureOpenAI
57+
58+
client = AsyncAzureOpenAI(
59+
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
60+
api_key=os.getenv("AZURE_OPENAI_KEY"),
61+
api_version="2024-02-01"
62+
)
63+
initialize_openai_agents_client(client)
64+
65+
# For Temporal workers, call this in worker startup:
66+
# run_worker.py:
67+
# initialize_openai_agents_client(client)
68+
# # Then start worker...
69+
70+
Note:
71+
If not called, the OpenAI Agents SDK uses default OpenAI configuration
72+
via the OPENAI_API_KEY environment variable.
73+
"""
74+
global _client_initialized
75+
76+
if _client_initialized:
77+
logger.warning(
78+
"OpenAI Agents SDK client already initialized. "
79+
"Ignoring subsequent initialization. "
80+
"This may indicate multiple workers or initialization calls."
81+
)
82+
return
83+
84+
# Runtime validation for async client
85+
if not isinstance(client, (AsyncOpenAI, AsyncAzureOpenAI)):
86+
raise TypeError(
87+
f"Client must be AsyncOpenAI or AsyncAzureOpenAI, got {type(client).__name__}. "
88+
f"The OpenAI Agents SDK requires async clients. "
89+
f"Use AsyncOpenAI instead of OpenAI."
90+
)
91+
92+
set_default_openai_client(client)
93+
logger.info(f"OpenAI Agents SDK client configured: {type(client).__name__}")
94+
95+
_client_initialized = True
96+
97+
98+
def is_openai_agents_client_initialized() -> bool:
99+
"""
100+
Check if custom OpenAI Agents SDK client has been initialized.
101+
102+
Returns:
103+
bool: True if initialize_openai_agents_client() has been called
104+
"""
105+
return _client_initialized
106+
107+
108+
def reset_openai_agents_client_initialization() -> None:
109+
"""
110+
Reset initialization flag (primarily for testing).
111+
112+
Warning: This does NOT reset the actual client in the agents library,
113+
only the initialization tracking flag in this module.
114+
"""
115+
global _client_initialized
116+
_client_initialized = False

0 commit comments

Comments
 (0)