Skip to content
English

Connecting OpenCode to the Claude API

A tutorial on configuring OpenCode to use the 1折AI Claude API relay, covering the endpoint address, API key, and models — including protocol selection, configuration examples, verification steps, and common errors.

Use OpenCode’s custom provider feature to connect to 1折AI. First, create an API key, and confirm the currently available models; the model IDs below are examples — adjust them according to your key’s permissions.

Official configuration verification date: 2026-10-15. The following configuration is compiled from the official documentation; end-to-end verification on this site has not yet been completed. Please first complete a brief Q&A and tool-call verification before using it for long-running tasks. OpenAI / GPT models are now live; see the model notes for the model list and pricing.

Based on official v1 Providers: run /connect, choose Other, enter onezhe as the provider ID, and fill in this site’s API key. Merge into the project’s opencode.json:

json
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"onezhe": {
"npm": "@ai-sdk/openai-compatible",
"name": "1折AI",
"options": { "baseURL": "https://1zheai.com/v1" },
"models": {
"claude-sonnet-4-6": { "name": "Claude Sonnet 4.6" }
}
}
}
}

Then use /models to select that provider’s model. /connect only stores credentials and does not replace the provider configuration. Here we explicitly select the SDK for Chat Completions; it must not be mistakenly swapped for @ai-sdk/openai, which is used for Responses. The official docs also allow referencing environment variables via options.apiKey: "{env:ONEZHE_API_KEY}".

Based on official v2 Providers, use the native v2 fields — they must not be mixed with the v1 fields above. Merge into the project’s opencode.jsonc:

json
{
"$schema": "https://opencode.ai/config.json",
"providers": {
"onezhe": {
"name": "1折AI",
"env": ["ONEZHE_API_KEY"],
"package": "@opencode/ai/providers/openai-compatible",
"settings": { "baseURL": "https://1zheai.com/v1" },
"models": {
"claude-sonnet-4-6": { "name": "Claude Sonnet 4.6" }
}
}
}
}

Provide ONEZHE_API_KEY in the environment of the process that launches OpenCode, then use /models to select onezhe/claude-sonnet-4-6. The main differences in v2 are providers, package, and settings, rather than v1’s provider, npm, and options. Please first use opencode --version to confirm the major version in use.

To use native Anthropic, v1 officially allows overriding provider.anthropic.options.baseURL, with examples including /v1; v2 lists @opencode/ai/providers/anthropic-compatible. This tutorial follows an explicit OpenAI-compatible configuration; you must not switch protocols based on the model name alone, nor assume that caching and tool calls have already been verified.

Verifying the Connection and Troubleshooting

Section titled “Verifying the Connection and Troubleshooting”
  1. Save and reload the configuration, and confirm that the currently selected provider is onezhe or the custom model you added.
  2. Send a short question and confirm it returns a response; then try a small, reversible task to separately verify tool calls and multi-turn conversation.
  3. Open the call logs and check the request time, model, status, and usage. A single successful text reply does not mean all Agent capabilities are compatible.
  4. If you encounter a 404, check whether the address and protocol in this article match; if you encounter a 401, check the key’s origin and whether the process read the environment variable; if you encounter a 429, review the rate-limit information before retrying. See error codes and troubleshooting for more cases.

Keys should only be stored in your local environment or the tool’s credential store — never commit them to a code repository, screenshots, or public logs. Background services may not inherit terminal environment variables; auxiliary models, search, and other tools may also have independent provider settings that need to be checked separately.

Check Claude API real-time pricing and billing rules. If you need other clients, return to the tool integration directory; if you are writing your own program, use the SDK and development guide.