DeepSeek API Integration Tutorial: OpenAI-Compatible Setup with CodeGateway
Most teams that pick up DeepSeek already have OpenAI-shaped client code. A service that used to call chat.completions.create with messages should not need a rewrite to start using a DeepSeek model, and in practice it does not: the switch usually touches three things — the base URL, the model identifier, and where the API key comes from. This tutorial walks that path end to end. You will create a CodeGateway key, point an OpenAI SDK client at the gateway, run a minimal curl request and a Python request, and then work through the error codes you are most likely to hit.
What this guide deliberately does not cover is DeepSeek's own client tooling or the separate guide on configuring the DeepSeek Harness desktop client, which lives in the DeepSeek Harness setup guide. Everything below is about calling a model over HTTP through a gateway, which is a different problem with a different set of gotchas.
Two things are worth deciding before you read further. First, the gateway does not use DeepSeek's public model names. Official DeepSeek entry points and gateway model identifiers are different strings, and mixing them up is the single most common reason a first request fails. Second, the numbers on DeepSeek's pricing page and the numbers on the gateway's pricing page are two different things: the first is DeepSeek's own direct pricing, the second is what you actually pay. This guide labels every figure with where it comes from and what date it was checked, so you can tell the two apart.
Step 1: Create an account and issue an API key
Start at the CodeGateway site and register an account. New accounts receive some trial credit, which is enough to run the examples below.
After signing in, open the Dashboard and go to the API keys page to create a key. The full value is shown only once, at creation time. Put it straight into a password manager or an environment variable, and keep it out of version control.
export CG_KEY="sk-cg-your-real-key-here"One key covers every model on the gateway. You do not register with each vendor, and you do not attach a separate payment card per vendor. If you would rather not assemble client configuration by hand, the configuration generator will emit the base URL and key fields for you.
If you are pointing several tools at the same gateway — an editor assistant, a terminal client, a background job — it is worth checking how each one reads its base URL before you copy values around. The Claude Desktop developer mode walkthrough covers the desktop-app case, and the Codex CLI setup guide covers the terminal case; both use the same gateway key and the same OpenAI-compatible base URL as the code below.
Step 2: Confirm the model IDs and how you will be charged
This is the step people rush through, and it is the step where most first-hour confusion comes from. DeepSeek publishes two entry points in its own API, and the gateway exposes its own identifiers for the same underlying models. As of the check date below, the gateway catalog lists these two DeepSeek models:
| Gateway model ID | Best fit | Cloudflare base price — input | Cloudflare base price — output | Cloudflare base price — cached input | Gateway context window |
|---|---|---|---|---|---|
deepseek-v4-flash | Everyday chat, tool calls, batch classification | $0.44 | $1.32 | $0.014 | 1,048,576 |
deepseek-v4-pro | Multi-step reasoning, complex code debugging, maths | $1.32 | $3.96 | $0.044 | 1,048,576 |
All prices are US dollars per million tokens. The three middle columns are the upstream Cloudflare Workers AI base prices, checked on 2026-08-14 against the DeepSeek V4 Flash model page and the DeepSeek V4 Pro model page. They are not your invoice. The gateway applies its own service rate on top of the upstream base price, so the number that matters to you is the one on the pricing page and on your Dashboard invoice. Every price figure in this guide is a Cloudflare base price unless it explicitly says otherwise.
The context window for both models is 1,048,576 tokens, confirmed on the gateway model catalog on 2026-10-29. Both models handle text, function calling and reasoning. Neither accepts image input; if you need to read images, pick a model in the catalog that lists vision support.
Flash or pro: the difference is one field
The two identifiers share an interface. Switching between them means changing the model string and nothing else — same request body, same response shape, same streaming events.
For routine work — summarisation, classification, everyday Q&A, tool calling — deepseek-v4-flash is the sensible default. Its upstream base price is lower (input $0.44 and output $1.32 per million tokens as of 2026-08-14, with your actual rate on the pricing page) and its context window is the same 1,048,576 tokens, so there is rarely a reason to pay up unless the task genuinely needs deeper reasoning. Reach for deepseek-v4-pro when you are debugging a multi-step problem, deriving a proof, or working through code that needs a longer chain of thought (Cloudflare base price: input $1.32, output $3.96 per million tokens).
On reasoning models the thinking process usually arrives in a separate reasoning_content field rather than in the main content string. Two practical consequences: do not render that field as if it were the final answer, and do not feed it back into the next turn's context on a retry — that inflates token consumption noticeably. DeepSeek's own documentation, checked on 2026-10-29, states that thinking mode is on by default and thinking strength defaults to high; the same source documents a default max_tokens of 8K in non-thinking mode and 64K in thinking mode, rising to 128K when reasoning_effort is set to max, with an upper bound of 384K on the Chat Completions endpoint.
Step 3: Point base_url at the gateway and run your first request
The gateway's OpenAI-compatible base URL is https://api.codegateway.dev/v1. Paths and request-body field names match OpenAI's.
Verify the connection with one curl call first:
curl https://api.codegateway.dev/v1/chat/completions \
-H "Authorization: Bearer $CG_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "deepseek-v4-flash",
"messages": [{"role": "user", "content": "Explain in one sentence what an API gateway does."}]
}'Then the same request from Python, using the official OpenAI SDK unchanged:
import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.codegateway.dev/v1",
api_key=os.environ["CG_KEY"],
)
resp = client.chat.completions.create(
model="deepseek-v4-flash",
messages=[
{"role": "system", "content": "You are a concise technical assistant."},
{"role": "user", "content": "Explain in one sentence what an API gateway does."},
],
max_tokens=512,
temperature=0.3,
)
print(resp.choices[0].message.content)If you are coming from DeepSeek's own SDK, the entire migration is those three lines: base_url, api_key, model. If you are coming from OpenAI's SDK, only base_url, api_key and the model identifier change; every call site, retry policy and parsing helper stays as it was.
Step 4: The errors you will actually hit
400 with "Unsupported model". The model identifier is not in the gateway catalog. The error body offers the nearest candidate in a "Did you mean ...?" form, and the model catalog lists the accepted spelling. The usual cause is pasting DeepSeek's own entry point straight into the request: the official side calls it deepseek-flash, while the gateway calls it deepseek-v4-flash.
401. The key is invalid, or the Authorization: Bearer <key> header is missing, or the key was pasted into front-end code and shipped to a browser. Check that the key is server-side only.
429. The request was rate limited. Back off and retry, and check the balance and call volume on the Dashboard usage page at the same time, because a rate limit and an exhausted balance look similar from the client side.
5xx, including 503 service unavailable. Either a transient upstream failure, or that model has no available route right now. Smart Routing is off by default, so the gateway will not silently hop to another vendor — if you want cross-vendor fallback you have to enable it per key in the Dashboard. The error troubleshooting guide covers the common client-side failure modes in more depth.
Timeout, or a response with empty content. Check whether max_tokens is set so small that the output is truncated, then check the usage page to see whether the request was billed. Retry with idempotency in mind so a repeated attempt does not charge twice.
Thinking text missing from content. Expected. Read the reasoning_content field separately when you want to surface it.
Official DeepSeek entry points versus gateway model IDs
DeepSeek's own documentation, checked on 2026-10-29 in the model and pricing reference and the Chat Completions endpoint reference, lists exactly two entry points: deepseek-flash and deepseek-v4-pro. They map to model versions DeepSeek-V4.1-Flash-1017 and DeepSeek-V4-Pro-0813 respectively, both with a 1M context length and a 384K maximum output.
| Official entry point | Official model version | Official context | Gateway identifier | Gateway context |
|---|---|---|---|---|
deepseek-flash | DeepSeek-V4.1-Flash-1017 (thinking and non-thinking) | 1M | deepseek-v4-flash | 1,048,576 |
deepseek-v4-pro | DeepSeek-V4-Pro-0813 | 1M | deepseek-v4-pro | 1,048,576 |
The flash pair is the trap, because the same string means different things on the two sides. DeepSeek's own docs note that the older names deepseek-v4-flash and deepseek-v4-flash-vision-exp are still accepted but that those models have been retired and are served by DeepSeek-V4.1-Flash-1017 at Flash pricing. On the gateway, deepseek-v4-flash is a live Workers AI native model identifier. So do not guess from the string — decide which side you are writing to: official side writes deepseek-flash, gateway side writes deepseek-v4-flash.
Two more corrections that are commonly written the wrong way round. The current official documentation no longer lists deepseek-chat or deepseek-reasoner at all; if older code hard-codes them, replace them with an identifier from the model catalog and leave the rest of the code alone. And when the official tables mention Responses API support, they mean DeepSeek's own interface — the gateway's two Workers AI native models are served through /v1/chat/completions.
Direct DeepSeek pricing and gateway pricing are separate ledgers; do not convert between them. DeepSeek quotes US dollars split into off-peak and peak rates, where off-peak is half the peak price and peak covers weekdays 09:00–12:00 and 14:00–18:00 Beijing time. Per million tokens as of 2026-10-29: for flash, cached input $0.003 off-peak and $0.006 peak, uncached input $0.15 and $0.30, output $0.6 and $1.2; for pro, cached input $0.022 and $0.044, uncached input $0.66 and $1.32, output $1.98 and $3.96. If you want to compare the two, read the official page and the pricing page separately, and do not subtract one set of numbers from the other.
Long context and cached input
A 1,048,576-token context window means a single request can carry a long document without trouble — cost still scales with the input tokens you send, so the useful lever is what you send repeatedly.
Put the stable part of the prompt first: the system prompt, the project background, a fixed set of example turns. Repeated content can be served from the cached input tier, which is billed at a lower rate (Cloudflare base price: $0.014 per million tokens for flash and $0.044 for pro, as of 2026-08-14; your actual rate is on the pricing page). Putting the variable part after the stable part raises the cache hit rate and makes the usage numbers on your dashboard easier to read. The mechanics of how the charge is calculated are in the billing guide.
Before you ship it
- Issue a dedicated key per environment so you can disable one and reconcile the other independently. Keep keys in environment variables or a secret manager, never in the repository.
- Assume the cache-hit tier matters: keep the stable system prompt at the front of the message list (Cloudflare base price: $0.014 flash, $0.044 pro per million tokens, as of 2026-08-14).
- Watch the usage and cost dashboard for the first few days and confirm the deductions match your expectation.
- Smart Routing is off by default and opt-in per key. With it off, a request goes only to the route named by
model.
Start without another vendor signup
If you have no DeepSeek key, or you would rather not register with each vendor and attach a separate foreign card to each account, create one CodeGateway key: register, issue an sk-cg- key on the Dashboard API keys page, then top up on the Dashboard billing page. That single key serves every model on the gateway, and usage and cost sit on one dashboard. Current rates are on the pricing page.
