---
title: "Connecting OpenCode to the Claude API"
description: "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."
headingIds: {"OpenCode v1 Configuration":"section-1","OpenCode v2 Configuration":"section-2","Verifying the Connection and Troubleshooting":"section-3","Pricing and Other Integration Methods":"section-4"}
---

<aside className="docs-publication" data-pagefind-ignore>

Documentation maintained by [1ZheAI](https://1zheai.com/en/editorial/#publisher). [Sources and corrections](https://1zheai.com/en/editorial/).

Found an error? [Contact support](mailto:support@1zheai.com) with the page address and a description. Do not send API keys or account credentials.

</aside>
Use OpenCode's custom provider feature to connect to 1折AI. First, [create an API key](/en/docs/api-keys/), and confirm the [currently available models](/en/docs/models/); the model IDs below are examples — adjust them according to your key's permissions.

<Note>
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](/en/docs/models/) for the model list and pricing.
</Note>

## OpenCode v1 Configuration

Based on [official v1 Providers](https://opencode.ai/docs/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}"`.

## OpenCode v2 Configuration

Based on [official v2 Providers](https://opencode.ai/v2/docs/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

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](/en/docs/usage-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](/en/docs/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.

## Pricing and Other Integration Methods

Check [Claude API real-time pricing](https://1zheai.com/en/pricing/) and [billing rules](/en/docs/pricing-explained/). If you need other clients, return to the [tool integration directory](/en/docs/integrations/); if you are writing your own program, use the [SDK and development guide](/en/docs/guides/).
