Connecting Cursor and opencode to a cheap AI API

cursor opencode api guide

Cursor, opencode and most coding agents have thing in common: whatever the UI looks like, they send an OpenAI-compatible request to chat/completions. That means you do not need a subscription per tool. You point the base URL at gateway, hand it key, and every tool gets the same models.

This guide covers Cursor, opencode and any OpenAI-compatible client, with the real model ids and today's prices.

Why change the base URL

Swapping the base URL changes where you pay and which models you can reach. It does not change the request shape, the response shape, or the SDKs in your project - which is why nothing in your tooling breaks.

"OpenAI-compatible" describes the protocol, not the models. The same request can go to DeepSeek, Gemini, GLM or GPT; you change field.

Before you start

  1. Create an account in the panel. The API key is shown - store it somewhere safe immediately.
  2. Top up. The minimum is 300,000 toman and Iranian cards are supported through Zarinpal.
  3. Keep the base URL handy: https://ai.layercloud.org/v1

Cursor

Open Settings, then Models:

  • Put your key in OpenAI API Key.
  • Enable Override OpenAI Base URL and set it to https://ai.layercloud.org/v1.
  • Add the model id by hand (Add model), for example deepseek/deepseek-v4.1-flash or zai/glm-5.3.

Two mistakes account for most failures here. Leaving off /v1 sends the request to the site root and returns 404. Pasting the display name ("DeepSeek V4.1 Flash") instead of the provider-prefixed id also fails; the id is what /v1/models returns. Restart Cursor after changing the base URL - some builds keep the model list in memory.

opencode

opencode reads opencode.json. Define custom provider:

{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "layercloud": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "LayerCloud",
      "options": {
        "baseURL": "https://ai.layercloud.org/v1",
        "apiKey": "{env:LAYERCLOUD_API_KEY}"
      },
      "models": {
        "deepseek/deepseek-v4.1-flash": {},
        "zai/glm-5.3": {}
      }
    }
  },
  "model": "layercloud/deepseek/deepseek-v4.1-flash"
}

Keep the key in the LAYERCLOUD_API_KEY environment variable rather than in the file, so committing the config does not leak it.

Test before you debug the editor

curl -sS https://ai.layercloud.org/v1/chat/completions \
  -H "Authorization: Bearer $LAYERCLOUD_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"deepseek/deepseek-v4.1-flash","messages":[{"role":"user","content":"hi"}]}'

If you get choices, the key and model are fine and anything left is editor configuration. A 401 means the key is wrong or revoked; a 404 means the model id or the missing /v1.

Which model for coding

Prices are USD per million tokens.

Model Input Output Context Good for
DeepSeek V4.1 Flash 0.0375 0.15 1M Everyday edits at volume
GLM 5.3 0.35 1.1 1.3M Large files and long rewrites
Gemini 3.8 Flash 0.1875 0.9375 1M Reading screenshots and pages
Kimi K3 0.75 3.75 1M Multi-step reasoning
GPT-5.5 1.25 7.5 1M When quality matters more than cost

Start on DeepSeek V4.1 Flash. It is the cheapest model that is genuinely good at editing code, and it lets you learn your own usage pattern before you spend on the expensive.

What a working day costs

A typical coding request is about 30k input and 4k output tokens. On DeepSeek V4.1 Flash that is roughly $0.0017, so 100 requests is about $0.17 - a month of normal work costs less than the minimum top-up. The same 100 requests on GPT-5.5 are about $6.70.

Common errors

Error Meaning Fix
401 Invalid or revoked key Create a new key and replace it
404 Wrong model id, or base URL without /v1 Read the id from /v1/models
429 Rate or quota limit Wait, or raise the token quota
400 Request shape is not compatible Switch the client to its OpenAI-compatible mode

Key hygiene

An API key is a password. Do not put it in project files, screenshots or commits. Create token per device with its own quota: if a laptop goes missing you revoke that token and everything else keeps working.

FAQ

Do I still need a Cursor subscription? Yes. Cursor charges for the editor; the gateway bills the model usage.

Why is my model missing from the list? The catalogue changes. /v1/models is always the authority and returns the exact ids.

Can Cursor and opencode share key? Yes, the key is not bound to a tool. Use separate tokens if you want to see spend per tool.

Responses are slow or drop. Switch models first to separate a model problem from a network problem, then check the status page.

More options and every price are on the pricing page. If you have not created an account yet, the guide to buying an AI API walks through the rial top-up and key creation step by step.