OpenAI Agents SDK with LiteLLM
OpenAI Agents SDK with LiteLLM
OpenAI의 Agents SDK를 LiteLLM Proxy를 통해 모든 LLM 제공자와 함께 사용해 봐요.
이 튜토리얼은 LiteLLM을 통해 여러 LLM 제공자를 지원하면서 OpenAI Agents SDK로 AI 에이전트를 구축하는 방법을 보여줍니다.
개요
OpenAI Agents SDK는 AI 에이전트 구축을 위한 고수준 인터페이스를 제공합니다. LiteLLM과 통합하면 다음을 할 수 있어요:
- 같은 에이전트 코드로 여러 LLM 제공자(Bedrock, Azure, Vertex AI 등) 사용
- 다른 제공자의 모델 간 손쉬운 전환
- 중앙 집중식 모델 관리를 위해 LiteLLM proxy에 연결
내장 LiteLLM 확장
OpenAI Agents SDK는 프록시 없이 동작하는 공식 LiteLLM 확장(LitellmModel)을 포함합니다. 중앙 집중식 프록시 기능(비용 추적, rate limiting, 로드 밸런싱)이 필요 없다면 직접 사용할 수 있어요:
from agents import Agent, Runnerfrom agents.extensions.models.litellm_model import LitellmModelagent = Agent(
name="Assistant",
instructions="You are a helpful assistant.",
model=LitellmModel(model="anthropic/claude-sonnet-5"),
)result = Runner.run_sync(agent, "Hello!")print(result.final_output)
자세한 내용은 문서를 참고하세요. 이 튜토리얼의 나머지 부분은 중앙 집중식 모델 관리가 필요한 팀을 위한 proxy 기반 접근에 초점을 맞춥니다.
전제 조건
- Python 환경 설정
- LLM 제공자용 API 키
- LLM과 에이전트 개념에 대한 기본 이해
설치
의존성을 설치합니다.
uv add openai-agents litellm
1. LiteLLM Proxy 시작
사용하려는 모델로 LiteLLM proxy를 구성하고 시작하세요:
config.yaml
model_list:
- model_name: bedrock-claude-sonnet-5
litellm_params:
model: "bedrock/us.anthropic.claude-sonnet-5"
aws_region_name: "us-east-1"
- model_name: gpt-5.6-terra
litellm_params:
model: "openai/gpt-5.6-terra"
- model_name: claude-sonnet-5
litellm_params:
model: "anthropic/claude-sonnet-5"
- model_name: bedrock-claude-opus
litellm_params:
model: "bedrock/us.anthropic.claude-opus-5"
aws_region_name: "us-east-1"
- model_name: bedrock-nova-premier
litellm_params:
model: "bedrock/amazon.nova-premier-v1:0"
aws_region_name: "us-east-1"
litellm --config config.yaml
필요한 환경 변수:
| Variable | Value | Description |
| LITELLM_BASE_URL | http://localhost:4000 | LiteLLM proxy URL |
| LITELLM_API_KEY | sk-<your-litellm-api-key> | Your LiteLLM API key (not your provider's key) |
2. 환경 설정
필요한 라이브러리를 가져오고 LiteLLM proxy 연결을 구성하세요:
Setup environment
from __future__ import annotationsimport asyncioimport osfrom openai import AsyncOpenAIfrom agents import (
Agent,
Model,
ModelProvider,
OpenAIChatCompletionsModel,
RunConfig,
Runner,
function_tool,
set_tracing_disabled,
)# Point to LiteLLM proxyBASE_URL = os.getenv("LITELLM_BASE_URL") or "http://localhost:4000"API_KEY = os.getenv("LITELLM_API_KEY") or "sk-"# Define model constants for cleaner codeMODEL_BEDROCK_SONNET = "bedrock-claude-sonnet-5"MODEL_BEDROCK_OPUS = "bedrock-claude-opus"MODEL_GPT = "gpt-5.6-terra"# Create the OpenAI client pointed at LiteLLMclient = AsyncOpenAI(base_url=BASE_URL, api_key=API_KEY)# Disable tracing since we're not using OpenAI's platform directlyset_tracing_disabled(disabled=True)
3. 커스텀 Model Provider 생성
Agents SDK는 모델 이름을 해석하기 위해 ModelProvider를 사용합니다. 모든 요청을 LiteLLM을 통해 라우팅하는 커스텀 provider를 만드세요:
Custom LiteLLM model provider
class LiteLLMModelProvider(ModelProvider): def get_model(self, model_name: str | None) -> Model: return OpenAIChatCompletionsModel(
model=model_name or MODEL_BEDROCK_SONNET,
openai_client=client,
)LITELLM_MODEL_PROVIDER = LiteLLMModelProvider()
4. 간단한 도구 정의
에이전트가 사용할 도구를 만드세요:
Weather tool implementation
@function_tooldef get_weather(city: str) -> str: """Retrieves the current weather report for a specified city.
Args:
city: The name of the city (e.g., "New York", "London", "Tokyo").
Returns:
A string containing the weather information for the city.
"""
print(f"[debug] getting weather for {city}")
mock_weather_db = {
"new york": "The weather in New York is sunny with a temperature of 25°C.",
"london": "It's cloudy in London with a temperature of 15°C.",
"tokyo": "Tokyo is experiencing light rain and a temperature of 18°C.",
}
city_normalized = city.lower()
if city_normalized in mock_weather_db:
return mock_weather_db[city_normalized]
else:
return f"Sorry, I don't have weather information for '{city}'."
5. 에이전트와 함께 다양한 모델 사용
5.1 Bedrock 모델 사용
Bedrock model via LiteLLM proxy
async def test_bedrock_agent(): print("\n--- Testing Bedrock Claude Agent ---")
agent = Agent(
name="weather_agent_bedrock",
instructions="You are a helpful weather assistant powered by Claude. "
"Use the 'get_weather' tool for city weather requests. "
"Present information clearly.",
tools=[get_weather],
)
result = await Runner.run(
agent,
"What's the weather in Tokyo?",
run_config=RunConfig(
model_provider=LITELLM_MODEL_PROVIDER,
model="bedrock-claude-sonnet-5", # Uses the model name from your LiteLLM config
),
)
print(f"
5.2 OpenAI 모델 사용
OpenAI model via LiteLLM proxy
async def test_openai_agent(): print("\n--- Testing OpenAI GPT Agent ---")
agent = Agent(
name="weather_agent_gpt",
instructions="You are a helpful weather assistant powered by gpt-5.6-terra. "
"Use the 'get_weather' tool for city weather requests. "
"Present information clearly.",
tools=[get_weather],
)
result = await Runner.run(
agent,
"What's the weather in London?",
run_config=RunConfig(
model_provider=LITELLM_MODEL_PROVIDER,
model="gpt-5.6-terra", # Uses the model name from your LiteLLM config
),
)
print(f"
5.3 Anthropic 모델 사용
Anthropic model via LiteLLM proxy
async def test_anthropic_agent(): print("\n--- Testing Anthropic Claude Agent ---")
agent = Agent(
name="weather_agent_claude",
instructions="You are a helpful weather assistant powered by Claude. "
"Use the 'get_weather' tool for city weather requests. "
"Present information clearly.",
tools=[get_weather],
)
result = await Runner.run(
agent,
"What's the weather in New York?",
run_config=RunConfig(
model_provider=LITELLM_MODEL_PROVIDER,
model="claude-sonnet-5", # Uses the model name from your LiteLLM config
),
)
print(f"
6. 완전한 동작 예시
복사해서 실행할 수 있는 완전한 엔드투엔드 스크립트입니다:
complete_agent.py
from __future__ import annotationsimport asyncioimport osfrom openai import AsyncOpenAIfrom agents import (
Agent,
Model,
ModelProvider,
OpenAIChatCompletionsModel,
RunConfig,
Runner,
function_tool,
set_tracing_disabled,
)# Point to LiteLLM proxyBASE_URL = os.getenv("LITELLM_BASE_URL") or "http://localhost:4000"API_KEY = os.getenv("LITELLM_API_KEY") or "sk-"MODEL_NAME = os.getenv("MODEL_NAME") or "bedrock-claude-sonnet-5"client = AsyncOpenAI(base_url=BASE_URL, api_key=API_KEY)set_tracing_disabled(disabled=True)class LiteLLMModelProvider(ModelProvider): def get_model(self, model_name: str | None) -> Model: return OpenAIChatCompletionsModel(
model=model_name or MODEL_NAME,
openai_client=client,
)LITELLM_MODEL_PROVIDER = LiteLLMModelProvider()@function_tooldef get_weather(city: str) -> str: """Retrieves the current weather report for a specified city."""
print(f"[debug] getting weather for {city}")
mock_weather_db = {
"new york": "The weather in New York is sunny with a temperature of 25°C.",
"london": "It's cloudy in London with a temperature of 15°C.",
"tokyo": "Tokyo is experiencing light rain and a temperature of 18°C.",
}
city_normalized = city.lower()
if city_normalized in mock_weather_db:
return mock_weather_db[city_normalized]
else:
return f"Sorry, I don't have weather information for '{city}'."async def main(): agent = Agent(
name="Assistant",
instructions="You are a helpful weather assistant. "
"Use the 'get_weather' tool for city weather requests. "
"Present information clearly and concisely.",
tools=[get_weather],
)
# Run with the default model (bedrock-claude-sonnet-5)
result = await Runner.run(
agent,
"What's the weather in Tokyo?",
run_config=RunConfig(model_provider=LITELLM_MODEL_PROVIDER),
)
print(result.final_output)
# Switch to a different model by passing model in RunConfig
result = await Runner.run(
agent,
"What's the weather in London?",
run_config=RunConfig(
model_provider=LITELLM_MODEL_PROVIDER,
model="gpt-5.6-terra",
),
)
print(result.final_output)if __name__ == "__main__": asyncio.run(main())
Agents SDK와 함께 LiteLLM을 쓰는 이유
| Feature | Benefit | | Multi-Provider | Use the same agent code with OpenAI, Bedrock, Azure, Vertex AI, etc. | | Cost Tracking | Track spending across all agent conversations | | Rate Limiting | Set budgets and limits on agent usage | | Load Balancing | Distribute requests across multiple API keys or regions | | Fallbacks | Automatically retry with different models if one fails |
관련 리소스
- OpenAI Agents SDK Documentation
- LiteLLM Proxy Quick Start