Configure coding agents
Coding agents such as OpenCode and Codex CLI run on your workstation, where they can read files, edit code, and run commands. You can route their model requests through the ConfidentialMind Model Gateway and give them access to gateway-hosted RAG tools.
This guide configures OpenCode with two gateway surfaces:
- The OpenAI-compatible API at
https://api.YOUR_DOMAIN/v1for model calls. - The remote MCP endpoint at
https://api.YOUR_DOMAIN/mcp/for RAG and other gateway-hosted tools.
OpenCode remains the agent loop. It executes local tools itself and calls the gateway's MCP endpoint when the model selects a gateway-hosted tool. Steps 1 to 4 are for OpenCode. For Codex CLI, see Configure Codex CLI.
Before you begin
To configure OpenCode, you need:
- OpenCode installed on your workstation.
- Your Model Gateway domain — shown as API URL at the bottom of the portal sidebar, next to a button that copies it.
- A personal API key with
invokegranted on the model you want to use. - For RAG tools, an API key with
readgranted on each RAG the agent may search. - A chat model that supports tool calling and is suitable for coding tasks.
A single key can carry both model and RAG grants. You can also use separate keys so model and RAG access can be rotated or revoked independently.
1. Export your API keys
Set the keys in the shell from which you will start OpenCode:
export CM_MODEL_API_KEY='cm_api_…'
export CM_RAG_API_KEY='cm_api_…'
If one key has both grants, export the same value for both variables. Keep the keys out of opencode.json and version
control.
2. Find the model ID
List the chat models the model key is allowed to invoke:
curl -sS "https://api.YOUR_DOMAIN/v1/models" \
-H "Authorization: Bearer $CM_MODEL_API_KEY"
Choose the exact id of a coding-capable model from the response. The display name is not interchangeable with the
ID.
3. Configure OpenCode
Create opencode.json in the root of your project. To use the provider for every project, put the same configuration
in ~/.config/opencode/opencode.json instead.
Replace YOUR_DOMAIN and MODEL_ID. The limits in these examples are for Qwen3.6 27B; use the served model's limits
when configuring a different model. For a model deployed on the platform, set limit.context to the
Context length shown on its page under Models in the portal. Consult the model's official page or model card
for its supported inputs, tool calling, reasoning options, and output limit, and confirm the limits configured on your
deployment. See
OpenCode's v1 model settings or
v2 model settings for how to express those settings in your config.
Choose the example for your installed version. If you are upgrading, follow OpenCode's Migrate from V1 guide.
OpenCode v1
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"cmgw": {
"npm": "@ai-sdk/openai-compatible",
"name": "CM Gateway",
"options": {
"baseURL": "https://api.YOUR_DOMAIN/v1",
"apiKey": "{env:CM_MODEL_API_KEY}"
},
"models": {
"MODEL_ID": {
"name": "Qwen3.6 27B via CM Gateway",
"limit": {
"context": 262144,
"output": 32768
}
}
}
}
},
"mcp": {
"cm-gateway": {
"type": "remote",
"url": "https://api.YOUR_DOMAIN/mcp/",
"enabled": true,
"oauth": false,
"headers": {
"Authorization": "Bearer {env:CM_RAG_API_KEY}"
}
}
}
}
OpenCode v2
V2 uses providers, package, settings, and mcp.servers. The aisdk: prefix retains the same AI SDK adapter as
in the v1 example. MCP servers connect by default, so the v1 enabled field is omitted.
{
"$schema": "https://opencode.ai/config.json",
"providers": {
"cmgw": {
"package": "aisdk:@ai-sdk/openai-compatible",
"name": "CM Gateway",
"settings": {
"baseURL": "https://api.YOUR_DOMAIN/v1",
"apiKey": "{env:CM_MODEL_API_KEY}"
},
"models": {
"MODEL_ID": {
"name": "Qwen3.6 27B via CM Gateway",
"limit": {
"context": 262144,
"output": 32768
}
}
}
}
},
"mcp": {
"servers": {
"cm-gateway": {
"type": "remote",
"url": "https://api.YOUR_DOMAIN/mcp/",
"oauth": false,
"headers": {
"Authorization": "Bearer {env:CM_RAG_API_KEY}"
}
}
}
}
}
In both examples, cmgw/MODEL_ID combines the local provider name with the model ID sent to the gateway.
@ai-sdk/openai-compatible uses /v1/chat/completions, including streaming and client-driven tool calls.
The cm-gateway MCP server exposes only the tools the RAG key is authorized to use. To expose RAG tools only and hide
other gateway-hosted MCP tools, change its URL to https://api.YOUR_DOMAIN/mcp/?kind=rag.
4. Verify the connections
Check that OpenCode loads the model. With v1, filter by provider:
opencode models cmgw
With v2, list models and look for cmgw/MODEL_ID:
opencode models
Check the MCP connection:
opencode mcp list
The output should show cm-gateway as connected.
Run a model-only check:
opencode run --model cmgw/MODEL_ID \
"Reply with: ConfidentialMind gateway reached"
Then verify a gateway-hosted RAG call:
opencode run --model cmgw/MODEL_ID \
"Use an available cm-gateway RAG search tool to answer a question about the indexed documents."
In interactive OpenCode, run opencode and use /models to select the model under CM Gateway.
How tool calls are routed
- OpenCode sends the conversation and available tool declarations to
/v1/chat/completions. - The model returns a tool call.
- OpenCode executes local tools on your workstation.
- For a
cm-gatewayMCP tool, OpenCode calls/mcp/; the gateway authorizes and executes the RAG call. - OpenCode returns the tool result to the model for the next step.
The model API key and RAG API key are checked independently. Giving the model key invoke does not grant RAG access,
and giving the RAG key read does not grant model access.
Configure Codex CLI
Codex CLI calls models through the gateway's Responses API. This section describes Codex CLI 0.171.0.
Export your model API key in the shell from which you will start Codex:
export CM_MODEL_API_KEY='cm_api_…'
Add the gateway as a model provider in ~/.codex/config.toml. Replace MODEL_ID with the exact id returned by
GET /v1/models (see Find the model ID) and YOUR_DOMAIN with your Model Gateway domain:
model_provider = "cmgw"
model = "MODEL_ID"
web_search = "disabled"
[model_providers.cmgw]
name = "CM Gateway"
base_url = "https://api.YOUR_DOMAIN/v1"
env_key = "CM_MODEL_API_KEY"
wire_api = "responses"
If you already have a config.toml, merge these settings into it: keep the three root keys above the first table, and
do not add a second [model_providers.cmgw] table.
Keep the provider set up as shown. These changes make Codex send requests the gateway rejects:
- Setting
nameto"OpenAI": every request fails with400(unsupported item type: "configuration_update"). - Using Codex's built-in OpenAI provider with
openai_base_urlset to the gateway: Codex connects over WebSocket, which the gateway does not serve, so every request fails. - Setting
nameto"azure"or settingcapabilities.remote_compaction = "v2": long sessions fail; see Long sessions.
Codex chooses the tools it declares from the model ID. For a model ID it does not recognize, it declares OpenAI-hosted
web search, its sub-agent tools and its MCP servers in forms the gateway accepts only for models provided by OpenAI
directly. To use such a model from another provider, keep web_search = "disabled", add no MCP servers, and add this
table to turn off the sub-agent tools:
[features]
multi_agent = false
Anthropic models do not work with Codex: Codex sends prompt_cache_key with every request, and the gateway rejects it
for these models.
For the gpt-6.x model IDs, Codex sends no hosted web search tool; to let Codex search the web on them, see Web search on gpt-6.x models.
Check the connection:
codex exec --skip-git-repo-check --ephemeral "Reply with: gateway reached"
This command checks the connection only; it does not test tool use.
For the gpt-6.x model IDs, Codex sends its tools in an additional_tools input item, which the gateway accepts for
direct models provided by OpenAI when the request sets store: false (see Use tools);
Codex already sends store: false. Codex sub-agents work on these models on the same terms: Codex sends the messages
between its agents as agent_message input items.
Gateway tools in Codex
Codex can use the gateway's tools only with a model provided by OpenAI directly, or an alias that resolves to one.
For other models, leave the MCP server out of the Codex configuration: the gateway rejects every request with 400.
To give Codex the RAG and other tools hosted by the gateway, export CM_RAG_API_KEY as in Export your API
keys and add the gateway's MCP endpoint to ~/.codex/config.toml:
[mcp_servers.cm-gateway]
url = "https://api.YOUR_DOMAIN/mcp/"
bearer_token_env_var = "CM_RAG_API_KEY"
default_tools_approval_mode = "approve"
default_tools_approval_mode = "approve" approves this server's tools in advance. If a tool call still requires
approval, codex exec can fail with tool required approval; see Troubleshooting. The endpoint
lists only the tools the key is authorized to use.
Long sessions
When a session gets close to the model's context limit, Codex compacts it: it asks the model to summarize the work so far, then continues from that summary. With the configuration above, Codex does this itself, and you do not need to change any setting.
If the provider is named azure or sets capabilities.remote_compaction = "v2", Codex instead asks the model provider
to compact the session, which the gateway does not support: the compaction request returns 400 (unsupported item type: "compaction_trigger"). If Codex needs to compact before or during a turn, that turn fails.
Web search on gpt-6.x models
On the gpt-6.x model IDs, Codex searches the web with its own web.run tool. Codex runs the tool itself and sends each
search to the provider's /v1/alpha/search endpoint, which the gateway forwards to OpenAI. Codex only offers this tool
to a custom provider that opts in. To turn it on, set web_search to "live" and add
supports_standalone_web_search to the provider table:
model_provider = "cmgw"
model = "gpt-6-astra"
web_search = "live"
[model_providers.cmgw]
name = "CM Gateway"
base_url = "https://api.YOUR_DOMAIN/v1"
env_key = "CM_MODEL_API_KEY"
wire_api = "responses"
supports_standalone_web_search = true
Use supports_standalone_web_search only with gpt-6.x model IDs.
Codex recognizes a model by the
start of its ID, so this applies to IDs that begin with a gpt-6.x model name, such as gpt-6-astra or
gpt-6-luna-openai. With web_search = "cached", or with
no web_search key, Codex asks OpenAI not to fetch live pages (external_web_access: false).
Keep in mind:
/v1/alpha/searchis an undocumented OpenAI endpoint that Codex uses. OpenAI can change it without notice.- The gateway forwards it only to models provided by OpenAI directly, or aliases that resolve to one. Any caller who
may use the model may search. The gateway forwards the members
id,model,input,commands,settings,reasoningandmax_output_tokens, withmodelchanged to the provider's model ID, and drops any others. - OpenAI bills searches to the account behind the model's connector. Searches are not recorded in platform usage, and token quotas do not limit them.
- Codex retries a search that fails with a server error up to four times, so a search that reaches the gateway's timeout (2 minutes by default) can hold a Codex turn for about 10 minutes.
Configure another coding agent
An agent must support both a custom OpenAI-compatible model provider and a remote streamable-HTTP MCP server:
| Integration | URL | Authentication | Selection |
|---|---|---|---|
| Model provider | https://api.YOUR_DOMAIN/v1 | API key with model invoke | Exact ID from GET /v1/models |
| Gateway tools | https://api.YOUR_DOMAIN/mcp/ | Bearer API key with RAG read | Tools are filtered by grants |
The coding agent remains responsible for the agent loop. The gateway routes model calls, federates authorized RAG tools, executes gateway-hosted tool calls, and records usage and audit events.
For Codex CLI, see Configure Codex CLI.
Claude Code is not supported. Claude Code sends a context_management setting, which the gateway rejects with HTTP
400 (context_management is not supported) before any model is called.
For VS Code agent mode, see Configure VS Code agent mode. VS Code uses the Custom Endpoint language model provider and manages the conversation on your machine.
Troubleshooting
| Symptom | Check |
|---|---|
Model request returns 401 | Key is exported as CM_MODEL_API_KEY, unexpired, and neither revoked nor disabled. |
Model request returns 403 | The model key has invoke granted on the configured model. |
| Model not found | MODEL_ID exactly matches an id returned by GET /v1/models, and the model is ready. |
cm-gateway is disconnected | The MCP URL ends in /mcp/ and the authorization header includes Bearer. |
| No RAG tools appear | The RAG is ready and the RAG key has read granted on it. |
| The model writes tool calls as text | Select a coding or instruction model with tool-calling support. |
| Context is truncated too early | Set limit.context to the model's Context length in the portal and limit.output to the output limit configured on your deployment. |
Codex request returns 400 with web search is supported only for OpenAI models | Set web_search = "disabled" in the Codex configuration, or choose a model provided by OpenAI directly. |
Codex request returns 400 with namespace tools are supported only for OpenAI models | Set multi_agent = false under [features] and remove MCP servers from the Codex configuration, or choose a model provided by OpenAI directly. |
Codex request returns 400 with prompt_cache_key is not supported for this model | Codex does not work with Anthropic models. Choose another model. |
| Codex on a gpt-6.x model never searches the web | supports_standalone_web_search = true is in the [model_providers.cmgw] table and web_search is not "disabled". See Web search on gpt-6.x models. |
Codex search returns 400 with does not support /v1/alpha/search | Choose a model the platform calls at OpenAI directly, or an alias that resolves to one. |
Codex search returns an error starting with http 401 or http 403 and OpenAI's message | The OpenAI key or account behind the model's connector was rejected, not your platform API key. Ask the platform administrator to check the connector. |
Codex request returns 400 with additional tools are supported only for OpenAI models | Choose a model the platform calls at OpenAI directly, or an alias that resolves to one, not an Azure OpenAI model, a model from another provider or a preset agent. Codex already sends store: false. See Use tools. |
Every Codex request returns 400 with unsupported item type: "configuration_update" | The provider's name is "OpenAI". Use another name, such as "CM Gateway". |
A long Codex session fails with 400 and unsupported item type: "compaction_trigger" | Set up the provider with the name and base_url shown in Configure Codex CLI, without capabilities.remote_compaction. See Long sessions. |
codex exec fails with tool required approval | Set default_tools_approval_mode = "approve" on the cm-gateway MCP server. See Gateway tools in Codex. |
A disabled key can be enabled again from the API Keys page. Enabling preserves expiry dates and grants; the secret must still be unexpired and have the required permissions.
For the complete configuration schema, see the OpenCode docs for your version:
- v1: Providers and MCP servers.
- v2: Providers and MCP servers.