Codex

Connect Codex to CloudZero to see its usage next to the rest of your cloud and AI spend. Codex exports its own OpenTelemetry traces, so you can send usage straight to CloudZero from the Codex CLI by adding a few lines to its configuration file. Once connected, you can break Codex usage down by the same Dimensions as your other spend, such as team, product, or environment.

This guide covers turning on Codex's trace export and pointing it at the dedicated CloudZero route.

Overview

Connecting Codex takes five steps, two of them optional:

  1. Create a CloudZero API key
  2. Turn on trace export and point it at CloudZero
  3. Tag usage by team or project (optional)
  4. Roll out org-wide (optional)
  5. Send traffic and verify

How it works

Codex exports OpenTelemetry traces using the OpenTelemetry Protocol (OTLP) over HTTP. Each time Codex finishes an agent turn, it emits a turn span carrying the model and that turn's token usage. CloudZero turns each turn span into one usage event. Sub-agent turns are recorded as their own events, and each turn's tokens cover only its own model calls, so nothing is counted twice.

Events appear on the Livestream tab of AI Signals within a few minutes of a completed turn.

The AI Collector also captures Codex usage, including the Codex desktop app, when Codex is signed in with a ChatGPT subscription. The Collector is an app you install on each Mac. This integration is a configuration change only, works wherever the Codex CLI runs, and does not depend on how Codex is signed in. Use one path per tool on each machine. Usage a tool reports both ways is counted twice.

⚠️

Codex must send to the dedicated /v2/telemetry/codex route. Codex does not use the OpenTelemetry GenAI attribute names, so the generic OTel route does not count its spans. Use the endpoint exactly as shown in Step 2.

ℹ️

This integration does not use the Create Connection flow in Settings > Connections. You connect it by creating a CloudZero API key and pointing Codex's trace export at CloudZero, as described below.

What you need

  • A CloudZero user with API key access
  • Permission to view AI Signals in CloudZero, to confirm data is arriving
  • The Codex CLI installed and in use by the team you are onboarding
  • Outbound HTTPS connectivity from every machine running Codex to telemetry.cloudzero.com on port 443

Step 1: Create a CloudZero API key

  1. In CloudZero, go to Settings > API Keys.
  2. Create a new API key.
  3. Under Scopes, choose Managed scopes and select Send AI Telemetry. CloudZero keeps the key's scopes current as new telemetry endpoints ship.
  4. Copy the key value and store it securely. You add it to the Codex configuration in Step 2. Treat it like any other secret.
ℹ️

Send AI Telemetry authorizes CloudZero's AI telemetry ingest endpoints. To grant it explicitly instead, choose Fixed scopes and select AI Telemetry (all sources) (ai-telemetry-ingest:ingest-ai-telemetry). A fixed key does not pick up telemetry endpoints added later. If a request returns 401, the key is invalid or has neither scope. See Fixed and managed scopes.

Step 2: Turn on trace export and point it at CloudZero

Codex's telemetry export is off by default. Add a trace exporter to the [otel] section of the user-level Codex configuration file, ~/.codex/config.toml:

[otel]
trace_exporter = { otlp-http = {
  endpoint = "https://telemetry.cloudzero.com/v2/telemetry/codex",
  protocol = "binary",
  headers = { "Authorization" = "<your-cloudzero-api-key>" }
}}
SettingValueWhy
trace_exporterotlp-httpExports traces over OTLP/HTTP. CloudZero's ingest does not accept gRPC, so do not use otlp-grpc.
endpointhttps://telemetry.cloudzero.com/v2/telemetry/codexThe CloudZero route for Codex.
protocolbinarySends OTLP as protobuf. CloudZero also accepts json.
headersAuthorization set to the key from Step 1Authenticates the export. CloudZero also accepts the key with a Bearer prefix.

Codex ignores [otel] in a project's .codex/config.toml. For every [otel] setting, see OpenAI's Codex configuration reference.

ℹ️

trace_exporter controls traces only. Codex's log export (exporter) and metrics export (metrics_exporter) are separate settings, and this integration does not need either of them.

Step 3: Tag usage by team or project (optional)

To slice Codex usage by your own business categories, add cz.context.<name> attributes with Codex's span_attributes setting. Codex adds them to every span it exports, and CloudZero records them as business context on each turn:

[otel]
span_attributes = { "cz.context.team" = "payments", "cz.context.department" = "engineering" }

These values apply to every Codex session that uses the file, so they suit stable values such as a user's team or department. For the names with a defined meaning in CloudZero, see Business enrichment.

Step 4: Roll out org-wide (optional)

Once you have confirmed data is flowing from one machine (Step 5), set the same [otel] values in Codex's managed requirements.toml so every user's Codex reports to CloudZero with nothing to set themselves:

  • macOS and Linux: /etc/codex/requirements.toml
  • Windows: %ProgramData%\OpenAI\Codex\requirements.toml

Managed values override local configuration, and the managed trace_exporter definition replaces the local one in full, including its headers. OpenAI documents managed [otel] settings for Codex CLI 0.166.0 and later. To deliver the settings through MDM on macOS, or for the full precedence rules, see OpenAI's managed configuration documentation.

Step 5: Send traffic and verify

  1. Add the configuration from Step 2, or deploy the managed configuration from Step 4, on a machine running Codex.
  2. Start a new Codex session and complete a few turns.
  3. In CloudZero, open AI Signals and select the Livestream tab.

Codex events appear on the Livestream tab within a few minutes of a completed turn. Codex batches its exports and flushes them when it exits, so a turn can appear after the session ends rather than the moment it finishes.

Smoke-test your API key

Before you wait on real usage, you can confirm your key works. An empty POST should return HTTP 202:

curl -i -X POST \
  "https://telemetry.cloudzero.com/v2/telemetry/codex" \
  -H "Authorization: <your-cloudzero-api-key>" \
  -H "Content-Type: application/x-protobuf" \
  --data-binary $'\x00'

Expected response:

HTTP/2 202
content-type: application/json

{"status":"accepted"}

A 202 confirms the key is valid and has the right scope. A 401 means the key is invalid or revoked, or lacks both the Send AI Telemetry managed scope and the AI Telemetry (all sources) (ai-telemetry-ingest:ingest-ai-telemetry) fixed scope, so re-check Step 1.

⚠️

A 202 proves authentication, not routing. CloudZero also returns 202 for an empty body or a mistyped route under /v2/telemetry/, and drops data it cannot process. The Livestream tab is what proves Codex's real traces are being ingested. If the smoke test passes but nothing appears after real usage, work through Troubleshooting.

What to expect

After you connect, CloudZero records one event per completed Codex turn on the AI Signals Livestream tab, showing the model, token counts, and timing. Events appear within a few minutes of a completed turn.

From there, you can organize Codex usage by team or product using Dimensions, track trends in Analytics Dashboards, or ask questions in the AI Hub.

What CloudZero records

For each Codex turn, CloudZero records:

FieldSource on the turn span
Modelmodel
Input tokens, including cached inputcodex.turn.token_usage.input_tokens
Cached input tokenscodex.turn.token_usage.cached_input_tokens
Cache write tokenscodex.turn.token_usage.cache_write_input_tokens
Output tokens, including reasoningcodex.turn.token_usage.output_tokens
Reasoning tokenscodex.turn.token_usage.reasoning_output_tokens
Start time, duration, and statusThe span itself

CloudZero also records the service name and version from the export, and any cz.context attributes you add in Step 3. Codex's spans do not identify the user.

CloudZero records every Codex turn with OpenAI as the provider. If you point Codex at a different model provider, its usage is still labeled OpenAI.

Data privacy

Codex's turn spans carry the model, token counts, and timing. They do not carry prompt or response text. Codex's prompt and response logging settings (such as log_user_prompt) apply only to its log export, which this integration does not use.

Troubleshooting

If data is not appearing on the AI Signals Livestream tab 5 minutes after a completed Codex session, work through these in order.

  1. Exporter set on logs instead of traces. The setting must be trace_exporter, not exporter. exporter controls Codex's log export, which CloudZero does not read on this route. Re-check Step 2.
  2. Config in the wrong file. Codex ignores [otel] in a project's .codex/config.toml. Put it in ~/.codex/config.toml or the managed requirements.toml.
  3. Wrong or mistyped route. The endpoint must be https://telemetry.cloudzero.com/v2/telemetry/codex (or the same URL with /v1/traces appended), not the generic /v2/telemetry/otel-genai. The generic route does not count Codex's spans, and CloudZero returns 202 for unrecognized routes, so neither mistake produces an error.
  4. gRPC exporter. CloudZero's ingest does not accept gRPC. Use otlp-http with protocol set to binary or json.
  5. Invalid key or missing scope (401). The key is mistyped, revoked, or lacks the Send AI Telemetry managed scope and the AI Telemetry (all sources) (ai-telemetry-ingest:ingest-ai-telemetry) fixed scope. Run the smoke test and re-check Step 1.
  6. Session still running. Codex flushes its exports when it exits. End the session and check again.
  7. Old Codex version or config not picked up. Codex reads its configuration at startup. Start a new session after any change. If the trace_exporter setting has no effect, update Codex and retry.
ℹ️

Have questions or feedback? Reach out to your account manager.


Did this page help you?