Call DeepSeek V4.1 with the OpenAI SDK in Python and Node.js
Use DeepSeek V4.1 from the official OpenAI SDKs by changing two settings. Python and Node.js examples, streaming, the 1M-token context and pricing.

If your code already talks to an OpenAI-style API, it can talk to DeepSeek V4.1 through API Store without a new library. The chat endpoint takes the OpenAI Chat Completions request unchanged, so the official OpenAI SDKs work once you point them somewhere else. Two settings change: the base URL and the API key. The rest of your code stays as it is.
What you need
- An API Store account with some balance.
- An API key from the API keys page. It starts with
sk-. - The OpenAI SDK for your language:
pip install openaiornpm install openai.
The base URL is https://api.apistore.cloud/v1.
Python
from openai import OpenAI
client = OpenAI(
base_url="https://api.apistore.cloud/v1",
api_key="sk-...", # better: read it from an environment variable
)
res = client.chat.completions.create(
model="deepseek-v4-1-flash",
messages=[
{"role": "system", "content": "You are a concise assistant."},
{"role": "user", "content": "Explain HTTP caching in three sentences."},
],
max_tokens=400,
)
print(res.choices[0].message.content)
print(res.usage) # prompt and completion token countsNode.js
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.apistore.cloud/v1",
apiKey: process.env.APISTORE_API_KEY,
});
const res = await client.chat.completions.create({
model: "deepseek-v4-1-flash",
messages: [{ role: "user", content: "Write a haiku about latency." }],
max_tokens: 200,
});
console.log(res.choices[0].message.content);Streaming
Add stream: true to receive the answer as it is written. To get token counts at the end of a stream, also ask for them with stream_options:
stream = client.chat.completions.create(
model="deepseek-v4-1-flash",
messages=[{"role": "user", "content": "List five uses of a hash map."}],
stream=True,
stream_options={"include_usage": True},
)
for chunk in stream:
if chunk.choices:
print(chunk.choices[0].delta.content or "", end="", flush=True)
elif chunk.usage:
print("\n", chunk.usage)The two DeepSeek models
| Model id | When to use it |
|---|---|
deepseek-v4-1-flash | Everyday chat, coding and agents |
deepseek-v4-1-flash-economy | When the lowest price matters most |
Both have a context window of about one million tokens (1,048,576) and can write up to 393,216 tokens in one answer. Both support streaming, tool calls and JSON output through response_format. To see what is on sale right now, call GET /models or open the models page.
What it costs
You pay per token from a prepaid balance. Input and output tokens have separate prices, and input read from cache is cheaper still. For deepseek-v4-1-flash the standard price is $0.21 per million input tokens and $0.84 per million output tokens. Outside peak hours it drops to $0.105 and $0.42. The economy model costs $0.168 per million input tokens at the standard price.
Peak and off-peak prices explains when the cheaper window applies.
Always set max_tokens
Before a request runs, its largest possible cost is held from your balance: the prompt plus max_tokens worth of output. If you leave max_tokens out, the hold is sized for the longest answer the model can write, and a low balance can turn that into a 402 insufficient_balance error even for a short question. Setting max_tokens to what you really need keeps the hold small. You are only ever charged for the tokens actually used.
Every request is traceable
Each response carries an x-request-id header, and each request shows on the Usage page with its tokens and cost. If something looks wrong, that id is what to send to support.
Using frameworks
Anything that accepts an OpenAI-compatible base URL works the same way: LangChain, LlamaIndex, the Vercel AI SDK and most agent frameworks. Look for a base_url or baseURL setting, and use the model id as written above. For editors and coding agents, there are step-by-step guides for Cursor, Cline and Claude Code.
Also in Tiếng Việt, Español, Português, Bahasa Indonesia, Français



