# Codex Integration

Use [OpenAI Codex](https://chatgpt.com/codex) — a coding assistant that lives in your terminal — with Treasure AI as the backend.

Treasure AI customers can use Codex with their existing [AI Suite credits](https://www.treasuredata.com/terms/ai-suite-credits-definition/) — no separate OpenAI subscription required for TD-routed traffic.

Experimental
`tdx codex` is an experimental integration. Behavior, flags, and defaults may change in future releases as the Codex CLI itself evolves.

## Why use it

- **Code faster with AI in your terminal.** Ask questions, refactor code, generate tests, or write new features — all in plain English, right where you already work.
- **One subscription for everything.** Reuse your Treasure AI account and [AI Suite credits](https://www.treasuredata.com/terms/ai-suite-credits-definition/) instead of paying for a separate OpenAI plan. Usage rolls up under your TD account, so billing and access control stay in one place.
- **Keeps your existing Codex setup intact.** If you already use Codex against OpenAI, that config is left completely alone. `tdx codex` only wires up the Treasure AI provider for the session it launches.
- **Automatic sign-in.** Codex re-uses your `tdx` login, so you don't manage a second set of API keys and tokens don't expire mid-session.
- **Works with your active profile.** Multi-account or multi-region users get routed to the right place based on the `tdx` profile they've selected.


## What it helps with

Once you launch Codex in a `tdx`-managed project, you can ask it to help with the tasks `tdx` is built for:

- **Marketing as code** — draft and edit [YAML definitions](/treasure-code/guide/marketing-as-code) for parent segments, audiences, journeys, and campaigns, then have Codex run `tdx` pull/push/validate for you.
- **Write SQL against your TD data** — ask for a Trino query over your databases and tables, refine it in the terminal, and paste it into `tdx query`.
- **Author workflows** — draft `.dig` workflow files that ingest data, transform tables, or trigger jobs on a schedule.
- **Define CDP journeys and audiences** — describe the audience or realtime journey you want, and have Codex produce (or edit) the YAML that `tdx` will apply.
- **Explore and evolve an existing TD project** — explain unfamiliar YAML, SQL, or workflow files, refactor them, add validation tests, and generate boilerplate for new segments or connections.


It also works for anything Codex is normally good at — reading unfamiliar code, refactoring, generating tests, debugging errors, and writing small scripts.

## Quick start

You'll need a Treasure AI account with AI Suite access.

```bash
# 1. Install Codex (once per machine)
curl -fsSL https://chatgpt.com/codex/install.sh | sh

# 2. Sign in to tdx (once per profile)
tdx auth setup

# 3. Launch Codex against Treasure AI
tdx codex
```

Prefer a different Codex installer? `brew install --cask codex` (macOS) and `npm install -g @openai/codex` (any platform) also work — see the [Codex CLI docs](https://developers.openai.com/codex/) for options.

## Examples

```bash
# Just launch Codex
tdx codex

# Start Codex with an initial prompt
tdx codex -- "Explain this code"

# Use a specific tdx profile — its region is used automatically
tdx --profile production codex

# Pick a model or pass any other Codex flag through
tdx codex -- --model gpt-5.5 "hello"
```

Anything after `--` is passed to Codex unchanged, so any option supported by the Codex CLI is available.

## Example prompts

Once you're inside a Codex session, you can ask things like:

```
> Add a workflow to periodically import data from S3 into mydb.orders

> Write a SQL query to find the top customers by revenue in the last 30 days

> Create a parent segment YAML for customers who purchased in the last 60 days

> Add an audience under this parent segment for high-value customers with an email opt-in

> Explain what this journey definition does, and add a stage for a re-engagement email

> Refactor this workflow so the S3 import runs hourly instead of daily
```

## Options

`tdx codex` accepts the standard `tdx` global options — most usefully `--profile <name>` to pick which account and region to route through, and `--verbose` to print the exact Codex command that gets launched. Everything after `--` is passed to Codex unchanged.

## Usage report

```bash
tdx usage
tdx usage --json
```

The report shows usage by profile and source across the shared tdx usage ledger. While `tdx codex` is running, it records new token events from Codex's rollout logs for the `td` provider in the current workspace, then flushes pending events when Codex exits. Resuming a session does not import its old history. Sessions that disable rollout persistence cannot be recorded.

Prices come from the account's `/v1/work/models` catalog, including cache and long-context rates. Catalogs refresh periodically and retain their last successful response for offline use. Models without catalog prices use bundled estimates. Amounts use current prices and are estimates, not historical invoices.

## Client identification

TD LLM requests use `X-TD-Client: tdx/codex` through session-scoped provider
headers. TD API requests made by child `tdx` commands inherit the same identity
through `TDX_CLIENT`. The User-Agent retains `tdx/<version>` and adds
`client=tdx/codex` in its comment; `X-TD-Client-Version` remains the tdx version.
Ordinary CLI requests remain identified as `tdx`.

Parent startup requests and OAuth token refresh requests are outside this
attribution. Forwarded Codex configuration overrides can replace the injected
provider headers; no persistent Codex configuration is changed.

## Troubleshooting

**Codex says it can't find the `tdx` command.**
Make sure `tdx` is installed globally and on your `PATH` (e.g. `npm install -g @treasuredata/tdx`), then rerun `tdx codex`.

**Codex authentication fails after `tdx codex` starts.**
Check your sign-in with `tdx status` — it shows whether the active profile is authenticated. If not, run `tdx auth setup` to sign in again, then relaunch Codex.

**Codex seems to be using the wrong account or region.**
The active profile is captured when Codex launches, so switching profiles in another shell doesn't retarget an already-running Codex. Quit Codex and relaunch it with the profile you want: `tdx --profile <name> codex`.

## Learn More

- [Codex CLI Documentation](https://developers.openai.com/codex/) — upstream CLI documentation
- [`tdx claude`](/ja/treasure-code/commands/claude) — the equivalent launcher for Claude Code