With a single Codex project, one model, one Key, and one directory are enough. Once you have more projects, problems show up: you change the global config for project A and project B changes with it; two Codex tasks edit code in the same directory at the same time; all projects share one Key, so when something goes wrong you can't tell who was calling; and eventually you wire Codex into GitHub Actions too.
This article breaks it into four layers: separate configuration with Profiles, separate Keys per project, separate files with Git worktree, and inject the Key through a Secret in CI with tightened permissions. All the configs were actually run on Codex CLI 0.162.1 connected to AI Code With.
The parameter notes in this article cite the official OpenAI docs, mainly Codex Advanced Configuration and Codex Configuration Reference.
1. Four Layers of Isolation: Config, Keys, Files, Permissions
| Isolation layer | What it solves | What to use |
|---|---|---|
| Config layer | Different projects use different models or parameters | Codex Profile (--profile) |
| Key layer | You know which project is making calls, and you only have to disable one Key when something goes wrong | One provider and one Key per project |
| File layer | Parallel tasks don't overwrite each other | Git worktree |
| Permission layer | Automation gets only the capabilities it needs | GitHub Secret + minimal permissions |
These four layers can't replace each other: a Profile can switch models, but it can't stop two tasks from editing the same files at the same time; a worktree isolates files, but it won't separate your Keys for you.
2. Write the Global config.toml First
Step one: in the console, create a separate Key for each project, with a name that shows its purpose, such as codex-site-dev and codex-admin-dev:
Step two: open ~/.codex/config.toml (on Windows, %USERPROFILE%\.codex\config.toml) and write the full base configuration. The default provider is aicodewith:
model_provider = "aicodewith"
model = "gpt-6.1-sol"
[model_providers.aicodewith]
name = "aicodewith"
base_url = "https://api.aicodewith.ai/chatgpt/v1"
wire_api = "responses"
experimental_bearer_token = "sk-你的Key"
With this service you don't need to set an API Key environment variable; the Key goes directly in experimental_bearer_token. For what each field means, see “Codex config.toml Configuration Guide”. For which one takes effect when an environment variable conflicts with this file, see “How to Configure Codex Environment Variables”.
Official docs, verbatim: model_provider is “Provider id from model_providers”; experimental_bearer_token is “Direct bearer token for the provider”. Source: Configuration Reference.
3. Layer One: Use Profiles to Switch Models per Project
A Profile is a separate file next to config.toml
In current Codex, a Profile is not a [profiles.xxx] section inside config.toml. It's a file under ~/.codex named “project-name.config.toml”, and it contains only the settings that differ from the base config.
Official docs, verbatim: “When you pass --profile profile-name, Codex loads ~/.codex/config.toml, then overlays ~/.codex/profile-name.config.toml. Profile names can contain letters, numbers, hyphens, and underscores.” Source: Advanced Configuration · Profiles.
For example, say the website project uses gpt-6.1-sol and the admin project uses gpt-5.6-terra. Create two files. The first is ~/.codex/site.config.toml:
model = "gpt-6.1-sol"
The second is ~/.codex/admin.config.toml:
model = "gpt-5.6-terra"
Launch with --profile from each project's directory. Both interactive mode and codex exec work:
codex --profile site
codex exec --profile admin "检查这次改动"
Tested: I ran codex exec once with each profile. The startup info showed model: gpt-6.1-sol and model: gpt-5.6-terra respectively, both replied normally, and they took 2.7 seconds and 9.6 seconds.
The old [profiles.xxx] syntax errors out immediately
Many older tutorials still put [profiles.xxx] in config.toml. The official docs say it is no longer read as of 0.134.0:
Official docs, verbatim: “In Codex 0.134.0 and later, --profile no longer reads [profiles.profile-name] from config.toml, and the top-level profile = "profile-name" selector is no longer supported.” Source: Advanced Configuration · Profiles.
In testing, I left [profiles.legacy] in config.toml and ran codex exec --profile legacy. Codex doesn't start and errors out directly (path shown with ~ notation):
Error loading config.toml: --profile `legacy` cannot be used while ~/.codex/config.toml contains legacy `profile = "legacy"` or `[profiles.legacy]` config; move those settings into ~/.codex/legacy.config.toml and remove the legacy profile selector/table.
Following the prompt, move the contents to ~/.codex/legacy.config.toml, then delete [profiles.legacy] from config.toml.
A misspelled profile name doesn't raise an error
This is the easiest trap to fall into. I ran codex exec --profile proj-x with no proj-x.config.toml under ~/.codex: Codex showed no error or warning and just ran with the base config. So after every launch, check the model: and provider: lines at the top and confirm they match the project you meant.
4. Layer two: give each project its own Key
One provider per project, Profile does the selecting
To use a different Key per project, add a provider block for each Key in config.toml, then pick it with model_provider in the profile file. Append a block after the base config from section 2:
[model_providers.aicodewith_admin]
name = "aicodewith"
base_url = "https://api.aicodewith.ai/chatgpt/v1"
wire_api = "responses"
experimental_bearer_token = "sk-后台项目的Key"
Then change ~/.codex/admin.config.toml to specify both the provider and the model:
model_provider = "aicodewith_admin"
model = "gpt-5.6-terra"
Now codex --profile admin uses the admin project's Key, and running without --profile still uses the default aicodewith.
In testing, I pointed a profile at a provider with a deliberately wrong Key. The startup info showed provider: aicodewith_b and the request returned 401. On the same machine, a profile pointing at the correct Key replied normally. That confirms the profile really does switch the Key.
A profile file can also override the Key directly
A profile file is a layer on top of config.toml, so you can also rewrite the Key of the same provider inside it:
[model_providers.aicodewith]
experimental_bearer_token = "sk-另一把Key"
In testing, this works too (a wrong Key returned 401). Of the two approaches, though, one provider block per project is clearer: opening config.toml shows you how many Keys there are, and the profile files hold no Key, so copying one to a teammate doesn't leak anything.
Naming Keys and routine checks
A Key's name should at least answer three questions: which tool, which project, which environment. For example, codex-site-dev, codex-admin-dev, codex-ci-review. Once a week, review each Key's usage log: are the models correct, is it still in use, and disable any Key nobody uses. With few projects, split by project. With many, merge by risk: keep production separate and let temporary experiments share one test Key.
5. Layer three: don't share a working directory across parallel tasks
Once config and Keys are separated, files are next. When two Codex tasks edit code in the same directory at the same time, the danger isn't just merge conflicts at the end. One task may keep building on top of the half-finished state left by the other. Git worktree gives each task its own directory and branch while sharing the same repository's Git data.
Give each task a worktree
git worktree add ../project-task-a -b codex/task-a main
git worktree add ../project-task-b -b codex/task-b main
git worktree list
Then have each Codex task work only inside its own directory. Name the directory after the task, not tmp1 or tmp2. In the task description you give Codex, state the paths it may change, the acceptance commands, and the shared config it must not touch.
A worktree only isolates files
Two worktrees can still fight over the same port 3000, the same dev database, or the same test account. Before going parallel, list the shared resources: which can be given different ports or test data, and which can only be written by one task. Lockfiles, database migrations, and code generation output conflict most easily, so designate one task as the sole writer.
Make each task a verifiable commit before merging
git -C ../project-task-a status
git -C ../project-task-a diff --check
Review the diff in the worktree and run the project's own tests before committing. Then review on the main branch before you merge or cherry-pick. When two tasks depend on each other, merge the base changes first, then rebase the later branch and rerun the tests.
Clean up when done
git worktree remove ../project-task-a
git worktree prune
Clean up only after you confirm the commits have landed on the target branch and the directory holds no changes you want to keep. If you just delete the folder, Git still keeps the worktree record.
6. Layer four: use Codex in GitHub Actions
Minimal setup: generate config.toml from a Secret
In the repository, go to Settings → Secrets and variables → Actions and create a Secret named AICODEWITH_API_KEY. Its value is the Key you created specifically for CI. Then, in the workflow, install Codex first, and use one step to generate config.toml from the Secret:
name: codex-review
on: pull_request
permissions:
contents: read
jobs:
review:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
- run: npm install -g @openai/codex
- name: Configure Codex
env:
AICODEWITH_API_KEY: ${{ secrets.AICODEWITH_API_KEY }}
run: |
mkdir -p ~/.codex
cat > ~/.codex/config.toml <<EOF
model_provider = "aicodewith"
model = "gpt-6.1-sol"
[model_providers.aicodewith]
name = "aicodewith"
base_url = "https://api.aicodewith.ai/chatgpt/v1"
wire_api = "responses"
experimental_bearer_token = "${AICODEWITH_API_KEY}"
EOF
- name: Run Codex
run: codex exec "只检查本次改动的错误处理,列出发现和证据,不修改文件。"
In testing, I ran the script from the Configure Codex step under a clean HOME, passing the Key in through an environment variable. The generated config.toml then ran codex exec, which returned a normal reply in 2.9 seconds.
A few key points:
- The Key should appear only in the Secret and in the file generated at runtime. Don't write it into the repository, and don't echo it into the logs.
- Start permissions at contents: read. GitHub's permissions control what the workflow token can do to the repository, while Codex's own sandbox controls which files the commands it runs can touch. Tighten both layers.
- Pull requests from forks can't access repository Secrets by default. Don't switch to a broader trigger just to make them run.
For details on the GitHub permissions field, see GitHub Docs: Authenticate with GITHUB_TOKEN.
Add write permissions together with the task
A read-only review needs only read access. When you want Codex to edit files, commit patches, or post comments, add the matching permission, and add only the one this task needs. A safer flow is for Codex to produce a report or patch first, have a person or a separate step check the diff and tests, and then let a controlled process commit it.
Write the Prompt as acceptance criteria
In CI, a prompt like "optimize the code a bit" is nearly impossible to verify. Spell out the target scope, the areas that must not change, the checks that must run, and the output format. If the rules are long, put them in a file in the repository, and that file itself should go through Review.
7. A complete example: three projects and one CI
| Scenario | How to launch | Key (provider) | File isolation |
|---|---|---|---|
| Website development | codex --profile site | codex-site-dev (aicodewith) | Separate worktree |
| Admin backend development | codex --profile admin | codex-admin-dev (aicodewith_admin) | Separate worktree |
| SEO scripts | codex --profile seo | codex-seo-ops (aicodewith_seo) | Separate branch |
| PR review | GitHub Actions | codex-ci-review (injected via Secret) | CI checkout |
Profiles handle config differences, Keys handle usage attribution, worktrees handle parallel code changes, and CI uses its own Secret with read-only permissions. In the end, delivery still comes down to the diff, the tests, and Review.
8. Common errors
401: Please provide a valid API Key in the request
When the Key in the provider a profile points to is wrong or has expired, the actual output is:
ERROR: unexpected status 401 Unauthorized: {"error":"请在请求中提供有效的 API Key。"}, url: https://api.aicodewith.ai/chatgpt/v1/responses
Check which provider: block appears in the startup output, then verify experimental_bearer_token in that same block of config.toml. For more cases, see "How to Fix Codex 401 Unauthorized".
Model doesn't exist or isn't live yet
If a profile names a model this site doesn't offer, the output looks like this:
ERROR: {"error":"模型 gpt-5.5 不存在或未上线,请前往 https://aicodewith.com/zh/dashboard/pricing 查看可用模型列表 (request_id: ...)"}
Change model in the profile file to a name that appears in the console's model list, for example gpt-6.1-sol.
--profile throws a legacy config error
You get this error when [profiles.xxx] is still left in config.toml. See section 3 for the original message and the fix.
FAQ
Can a profile use its own Key?
Yes. The recommended approach is one provider block per Key, then pick it with model_provider in the profile file. You can also override experimental_bearer_token for the same provider directly in the profile file. Both worked in testing.
Why isn't my [profiles.work] taking effect?
Since 0.134.0, Codex no longer reads [profiles.xxx] in config.toml. Move the contents to ~/.codex/work.config.toml and delete the old section.
What happens if I mistype the --profile name?
In testing, it doesn't error. Codex just runs with the base config. After launch, check the model: and provider: lines at the top to confirm.
Do I need to set an API Key environment variable for multiple projects?
No. The Key lives in experimental_bearer_token under each provider in config.toml, and the profile handles selection. Only in CI do you inject the environment variable from a Secret first, then generate config.toml.
If I only have one Key, are Profiles still useful?
Yes. You can use a Profile just to switch models, for example gpt-6.1-sol for daily work and a faster model for simple tasks.
Do other tools (OpenCode, WorkBuddy) need per-project setups too?
The idea is the same. For the specific configuration, see "How to Configure Models in Codex, OpenCode, and WorkBuddy".
Sign up for AI Code With for free and set up a dedicated Key for Codex →
Related reading
- "Codex config.toml Configuration Guide"
- "How to Set Codex Environment Variables"
- "How to Configure the Codex API Key"
- "How to Configure Models in Codex, OpenCode, and WorkBuddy"
- "How to Fix Codex 401 Unauthorized"