API & SDKGuide
Anthropic Python SDK with CheapTokens: Messages API
Install the Anthropic Python SDK, point base_url at CheapTokens, send a messages.create request, and set max_tokens.

On this page
If your code already uses the Anthropic Python SDK, you do not have to rewrite it to call another model. CheapTokens speaks the Anthropic Messages API, so you change the client settings and keep client. as it is. This guide takes you from an empty folder to a script that prints an answer.
What you need#
- Python 3.10 or newer (the SDK's requirement).
- A CheapTokens API key. If you do not have one yet, see "What is an API key and how to store it safely".
- One balance covers every model, and there are no peak hours, so the same script costs the same at any time of day.
Install the SDK#
pip install anthropic
export CHEAPTOKENS_API_KEY="your-key"Keep the key in an environment variable, not in the source file.
Create the client#
Two settings differ from a default Anthropic setup: the key and base_url.
import os
import anthropic
client = anthropic.Anthropic(
api_key=os.environ["CHEAPTOKENS_API_KEY"],
base_url="https://api.cheaptokens.dev",
)The SDK appends the /v1/messages path itself, so base_url is the host without /v1. (The OpenAI-style endpoint, https:/, is the one you use with the OpenAI SDK; see "Your first API request with CheapTokens".)
First request#
message = client.messages.create(
model="deepseek-ai/DeepSeek-V4-Flash-0731",
max_tokens=1024,
messages=[
{"role": "user", "content": "Explain what a token is in two sentences."}
],
)
print(message.content[0].text)
print(message.stop_reason, message.usage.input_tokens, message.usage.output_tokens)Take the model id from the CheapTokens documentation; ids differ between providers. Here we use DeepSeek-V4-Flash: a 380K context window and up to 32,768 tokens in one answer, which suits long replies and agent loops.
The reply is in message.content, a list of blocks; the text is in the first one. usage shows how many input and output tokens the call used, which is what you are billed for.
max_tokens is required#
Unlike the OpenAI API, messages.create will not run without max_tokens. It caps the length of the answer:
- Too low, and the answer is cut off.
message.is thenstop_ reason "max_tokens"instead of"end_turn". - For DeepSeek-V4-Flash you can go up to 32,768 for long answers such as full files or reports.
- You pay for tokens actually generated, not for the limit you set.
If an answer stops mid-sentence, read "Why does an AI answer stop mid-sentence?" for the full explanation.
Switching models#
Only the model string changes; the rest of the script stays the same. Pick by task:
- DeepSeek-V4-Flash: long answers and agent loops, one price at any hour, up to 32,768 output tokens.
- GLM-5.3-Flash: cheap everyday chat and coding calls, 390K context, up to 8,192 output tokens.
- MiniMax-M2.7: low-cost agent and coding work, 180K context, up to 8,192 output tokens.
The list of available ids is returned by GET https:/, for example zai- and MiniMaxAI/.
Current prices per 1M tokens for MiniMax-M2.7 are $0.05 for input and $0.15 for output. MiniMax-M2.7 is cheaper than OpenRouter in total, with input and output counted together. Here is what a request with 2,000 input and 500 output tokens costs on each model, live from the price list:
| Model | Input $/1M | Output $/1M | Cost | OpenRouter |
|---|---|---|---|---|
| GLM-5.3-Flash | $0.02 | $0.04 | $0.00006 | $0.00012 |
| DeepSeek-V4-Flash | $0.015 | $0.10 | $0.00008 | $0.00017 |
| MiniMax-M2.7 | $0.05 | $0.15 | $0.00018 | $0.0012 |
2,000 input and 500 output tokens in total. Prices per 1M tokens are current and change together with our tariffs; the OpenRouter column shows reference prices for the same workload.
GLM-5.3-Flash and MiniMax-M2.7 are cheaper than OpenRouter in total, with input and output counted together.
Next step#
Create a key and run the script from the First request section. When it prints an answer, swap the model string to compare models on your own task.



