> ## Documentation Index
> Fetch the complete documentation index at: https://novita.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenAI Agents SDK

> Integrieren Sie Novita AI nahtlos mit dem OpenAI Agents SDK, um Multi-Agent-Workflows zu erstellen.

Das [OpenAI Agents SDK](https://github.com/openai/openai-agents-python) ist ein leichtgewichtiges und dennoch leistungsstarkes Framework zum Erstellen von Multi-Agent-Workflows. Das SDK ist mit allen Modellanbietern kompatibel, die das Format der OpenAI Chat Completions API unterstützen.

Diese Anleitung führt Sie durch die Verwendung der Novita LLM API mit dem OpenAI Agents SDK.

## Erste Schritte

1. Richten Sie Ihre Python-Umgebung ein und installieren Sie das Agents SDK.

```bash theme={"system"}
python -m venv env
source env/bin/activate
pip install openai-agents==0.0.7
```

2. Richten Sie Ihren Novita API-Schlüssel ein.

Gehen Sie zu [Key Management](https://novita.ai/settings/key-management?utm_source=getstarted) und erstellen Sie einen neuen API-Schlüssel.

## Hello-World-Beispiel

<Warning>
  Wenn Sie dies ausführen, stellen Sie sicher, dass die Umgebungsvariablen NOVITA\_API\_KEY und MODEL\_NAME gesetzt sind.
</Warning>

```python theme={"system"}
import os
from openai import AsyncOpenAI
from agents import (
    Agent,
    Runner,
    set_default_openai_api,
    set_default_openai_client,
    set_tracing_disabled,
)

BASE_URL = "https://api.novita.ai/openai"
API_KEY = os.getenv("NOVITA_API_KEY")
MODEL_NAME = os.getenv("MODEL_NAME")

# Because Novita not support the responses API so we use the chat completions API instead.
set_default_openai_api("chat_completions")
set_default_openai_client(AsyncOpenAI(base_url=BASE_URL, api_key=API_KEY))
# Disable tracing for this example
# Refer to https://openai.github.io/openai-agents-python/tracing/#external-tracing-processors-list to use the custom spans.
set_tracing_disabled(disabled=True)

agent = Agent(name="Assistant",
              instructions="You are a helpful assistant", model=MODEL_NAME)

result = Runner.run_sync(
    agent, "Write a haiku about recursion in programming.")
print(result.final_output)

# Code within the code,
# Functions calling themselves,
# Infinite loop's dance.
```

## Handoffs-Beispiel

<Warning>
  Wenn Sie dies ausführen, stellen Sie sicher, dass die Umgebungsvariablen NOVITA\_API\_KEY und MODEL\_NAME gesetzt sind.
</Warning>

```python theme={"system"}
import os
import asyncio
from openai import AsyncOpenAI
from agents import (
    Agent,
    Runner,
    set_default_openai_api,
    set_default_openai_client,
    set_tracing_disabled,
)

BASE_URL = "https://api.novita.ai/openai"
API_KEY = os.getenv("NOVITA_API_KEY")
MODEL_NAME = os.getenv("MODEL_NAME")

# Because Novita not support the responses API so we use the chat completions API instead.
set_default_openai_api("chat_completions")
set_default_openai_client(AsyncOpenAI(base_url=BASE_URL, api_key=API_KEY))
# Disable tracing for this example
# Refer to https://openai.github.io/openai-agents-python/tracing/#external-tracing-processors-list to use the custom spans.
set_tracing_disabled(disabled=True)

spanish_agent = Agent(
    name="Spanish agent",
    instructions="You only speak Spanish.",
    model=MODEL_NAME,
)

english_agent = Agent(
    name="English agent",
    instructions="You only speak English",
    model=MODEL_NAME,
)

triage_agent = Agent(
    name="Triage agent",
    instructions="Handoff to the appropriate agent based on the language of the request.",
    handoffs=[spanish_agent, english_agent],
    model=MODEL_NAME,
)


async def main():
    result = await Runner.run(triage_agent, input="Hola, ¿cómo estás?")
    print(result.final_output)
    # ¡Hola! Estoy bien, gracias por preguntar. ¿Y tú, cómo estás?


if __name__ == "__main__":
    asyncio.run(main())
```

## Functions-Beispiel

<Warning>
  Wenn Sie dies ausführen, stellen Sie sicher, dass die Umgebungsvariablen NOVITA\_API\_KEY und MODEL\_NAME gesetzt sind.
</Warning>

```python theme={"system"}
import os
import asyncio
from openai import AsyncOpenAI
from agents import (
    Agent,
    Runner,
    set_default_openai_api,
    set_default_openai_client,
    set_tracing_disabled,
    function_tool,
)

BASE_URL = "https://api.novita.ai/openai"
API_KEY = os.getenv("NOVITA_API_KEY")
MODEL_NAME = os.getenv("MODEL_NAME")

# Because Novita not support the responses API so we use the chat completions API instead.
set_default_openai_api("chat_completions")
set_default_openai_client(AsyncOpenAI(base_url=BASE_URL, api_key=API_KEY))
# Disable tracing for this example
# Refer to https://openai.github.io/openai-agents-python/tracing/#external-tracing-processors-list to use the custom spans.
set_tracing_disabled(disabled=True)

@function_tool
def get_weather(city: str) -> str:
    return f"The weather in {city} is sunny."

agent = Agent(
    name="Hello world",
    instructions="You are a helpful agent.",
    tools=[get_weather],
    model=MODEL_NAME,
)

async def main():
    result = await Runner.run(agent, input="What's the weather in Tokyo?")
    print(result.final_output)
    # The weather in Tokyo is sunny.

if __name__ == "__main__":
    asyncio.run(main())
```
