# OpenAI Agents SDK

seekrit gives an OpenAI Agents SDK agent its credentials without a `.env` file: wrap the process
with `seekrit run` and the SDK keeps reading `OPENAI_API_KEY` and `OPENAI_BASE_URL` from the
environment as it always has. Routing it through `seekrit-proxy` instead is configuration-only
too — point `OPENAI_BASE_URL` at the proxy and set `OPENAI_API_KEY` to a `{{seekrit:NAME}}`
placeholder — but watch the tracing exporter, which quietly uses the same key on a different path.

> **Note:** **Looking for the hosted Agents API?** This page is the in-process library
>   (`pip install openai-agents`), where your process runs the loop and wrapping it
>   is the whole integration. If OpenAI runs the loop and provisions the sandbox,
>   see [OpenAI Agents API](/docs/guides/frameworks/openai-agents-api) — the
>   credential path there depends on `environment.type`.

> **Note:** **Start here if seekrit is new:** [three
>   commands](/docs/guides/frameworks#get-running-in-five-minutes) put the keys in
>   an environment, mint a token bound to it, and export `SEEKRIT_TOKEN`. A token
>   reads [everything in its
>   environment](/docs/guides/frameworks#which-secrets-does-the-agent-get), so
>   there is no per-key or per-framework setup to do before any of the below.

## 1. Wrap the process

```bash
seekrit run -- python agent.py
```

```python
from agents import Agent, Runner

agent = Agent(name="Assistant", instructions="You are a helpful assistant")
result = Runner.run_sync(agent, "Write a haiku about recursion.")
print(result.final_output)
```

No seekrit-specific code at all — the key arrives in the environment the SDK
already reads.

## 2. Resolve in code

```python
import seekrit
from agents import set_default_openai_key

set_default_openai_key(seekrit.Client().get("OPENAI_API_KEY"))
```

Set it before the first model call. For a fully custom client — a different
endpoint, an org header, a shared `httpx` client:

```python
import seekrit
from openai import AsyncOpenAI
from agents import set_default_openai_client

secrets = seekrit.Client().resolve()
set_default_openai_client(AsyncOpenAI(api_key=secrets["OPENAI_API_KEY"]))
```

Pass either `openai_client` **or** `api_key`/`base_url`, never both — combining
them raises `UserError` rather than silently ignoring one.

## 3. Never hold the key

Environment only, no source change:

```bash
export OPENAI_BASE_URL=http://127.0.0.1:8080/openai/v1
export OPENAI_API_KEY='{{seekrit:OPENAI_API_KEY}}'
```

```toml
# seekrit-proxy.toml
listen = "127.0.0.1:8080"

[[route]]
prefix = "/openai"
upstream = "https://api.openai.com"
allow = ["OPENAI_API_KEY"]
methods = ["POST"]
paths = ["/v1/responses", "/v1/chat/completions"]
```

### Without running the proxy

`set_default_openai_client` takes a client, and a client takes a transport:

```python
import httpx
from openai import AsyncOpenAI
from agents import set_default_openai_client
from seekrit.transport import AsyncSeekritTransport

set_default_openai_client(
    AsyncOpenAI(
        api_key="{{seekrit:OPENAI_API_KEY}}",
        http_client=httpx.AsyncClient(
            transport=AsyncSeekritTransport(allow={"api.openai.com": ["OPENAI_API_KEY"]}),
        ),
    )
)
```

Weaker than the proxy, since it runs in your process:
[in-process injection](/docs/guides/agent-proxy/in-process) sets out the
trade-off.

## Gotchas

- **Tracing uses the same credential, to a different path.** The SDK exports
  traces to OpenAI by default with your API key. Behind the proxy that means
  trace uploads carry the placeholder too — so either allow the trace ingest
  path in your route, give tracing its own credential with
  `set_tracing_export_api_key(...)`, or turn it off with
  `set_tracing_disabled(True)`. If traces vanish the moment you add the proxy,
  this is why.
- **`OPENAI_WEBSOCKET_BASE_URL` is separate.** Realtime/websocket transport does
  not follow `OPENAI_BASE_URL`. The proxy substitutes on HTTP requests, so
  websocket traffic needs its own decision — usually: don't put a placeholder in
  that path.
- **Handoffs share the process.** Every agent in a handoff chain sees the same
  environment. Per-agent credential separation needs the proxy's session
  tickets, not a second env var.

## Common questions

### How do I give an OpenAI Agents SDK agent an API key without a .env file?

Wrap the process with seekrit run -- python agent.py. The Agents SDK already reads OPENAI_API_KEY from the environment, so no seekrit-specific code is needed — seekrit just fills the environment instead of a .env file.

### How do I keep the OpenAI key out of my Agents SDK process entirely?

Export OPENAI_BASE_URL pointed at seekrit-proxy and OPENAI_API_KEY as the placeholder {{seekrit:OPENAI_API_KEY}}, no source change required. Add a seekrit-proxy.toml route allowlisting that secret toward api.openai.com, matching the SDK's /v1/responses and /v1/chat/completions paths.

### Why did tracing stop working after I put the Agents SDK behind seekrit-proxy?

The SDK exports traces to OpenAI using the same API key by default, so behind the proxy trace uploads carry the placeholder too, and fail unless you've allowed the trace ingest path. Either allow that path in your route, give tracing its own credential with set_tracing_export_api_key(...), or turn tracing off with set_tracing_disabled(True).

### Does OPENAI_BASE_URL route websocket or Realtime traffic through seekrit-proxy too?

No. OPENAI_WEBSOCKET_BASE_URL is separate and does not follow OPENAI_BASE_URL. The proxy only substitutes on HTTP requests, so Realtime/websocket traffic needs its own decision — usually, don't put a placeholder in that path.

### Can two agents in an Agents SDK handoff chain hold different credentials?

Not through environment variables — every agent in a handoff chain shares the same process environment. Separating credentials per agent needs the proxy's session tickets, not a second env var.
