DEV Community

Super Lewis
Super Lewis

Posted on Fully Autonomous

Calling Qwen 4 Max from Python: streaming, real token usage, cost per request, and why it times out

Qwen 4 Max is Alibaba's flagship language model (model ID qwen4-max): a one-million-token context, text, image and video input, and thinking switched on by default. This tutorial shows how to call it with the OpenAI SDK, read real token usage from a stream, estimate what a request costs, and avoid the timeout most people hit first.

Disclosure: the measurements below come from apimodels.app, which is the API I work on. apimodels.app is a multi-model API gateway: one key and one OpenAI-compatible endpoint for image, video, audio and language models. The code works against any OpenAI-compatible endpoint that serves qwen4-max. I wrote this from my own test data, with AI assistance for drafting and editing.

Setup

  • Python 3.10+
  • pip install openai
  • LLM_API_KEY and LLM_BASE_URL in your environment (the base URL ends in /v1)
  • Verified on November 17, 2026

Stream everything

Qwen 4 Max finishes thinking before it writes. A non-streaming request sends nothing down the connection while that happens, and a proxy with a 60-second idle timeout will cut it. Stream, and give the client a generous timeout.

import os, time
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["LLM_API_KEY"],
    base_url=os.environ["LLM_BASE_URL"],
    timeout=600,
)

def ask(prompt: str, max_tokens: int = 20000):
    t0 = time.time()
    first_token_at = None
    parts, usage = [], None
    stream = client.chat.completions.create(
        model="qwen4-max",
        messages=[{"role": "user", "content": prompt}],
        max_tokens=max_tokens,
        stream=True,
        stream_options={"include_usage": True},
    )
    for chunk in stream:
        if chunk.usage:                 # the final chunk carries usage
            usage = chunk.usage
        if chunk.choices and chunk.choices[0].delta.content:
            if first_token_at is None:
                first_token_at = time.time() - t0
            parts.append(chunk.choices[0].delta.content)
    return "".join(parts), usage, first_token_at, time.time() - t0
Enter fullscreen mode Exit fullscreen mode

Two details that bite:

  • Without stream_options={"include_usage": True} a stream carries no usage at all.
  • The chunk that carries usage has an empty choices list, so check chunk.choices before indexing it.

Read the usage

completion_tokens already includes thinking tokens. If the endpoint returns the detail field, you can split them:

def split_output(usage):
    details = getattr(usage, "completion_tokens_details", None)
    thinking = (getattr(details, "reasoning_tokens", 0) or 0) if details else 0
    return thinking, usage.completion_tokens - thinking
Enter fullscreen mode Exit fullscreen mode

The widest split I saw: 10,777 thinking tokens for a 24-token answer, on a set of counting puzzles.

Estimate the cost

List prices per million tokens: $1.50 input, $4.50 output, $0.075 for cached input. Thinking is billed as output.

def cost_usd(usage, p_in=1.50, p_out=4.50, p_cached=0.075):
    details = getattr(usage, "prompt_tokens_details", None)
    cached = (getattr(details, "cached_tokens", 0) or 0) if details else 0
    fresh = usage.prompt_tokens - cached
    return (fresh * p_in + cached * p_cached
            + usage.completion_tokens * p_out) / 1_000_000
Enter fullscreen mode Exit fullscreen mode

Reference points from seven graded tasks, thinking on:

  • Chat log to JSON: 19 s, about $0.004
  • Fix a cache class with five bugs: 27 s, about $0.009
  • Write a function against 50 hidden tests: 51 s, about $0.012
  • Five questions over about 57K tokens of logs: 63 s, about $0.120
  • Three counting puzzles: 196 s, about $0.049

All 93 programmatic checks across the seven tasks passed, in 415 seconds and about $0.21 in total.

Troubleshooting

The request dies at around 60 seconds with no output. A non-streaming call hit an idle timeout. Switch to streaming and raise the client timeout. Time to first token was 15 to 62 seconds on most tasks and 196 seconds on the puzzles.

The bill is higher than you estimated. Print completion_tokens. Most of it is thinking, billed at $4.50 per million. For work that needs no multi-step reasoning, turn thinking off for that request, then check accuracy again.

No usage in the stream. You either left out stream_options or skipped the final chunk because its choices list was empty.

Invented details in generated copy. In my test it added a product weight and a charging time I never supplied. Put the real specs in the prompt and tell it to use only those.

When not to use Qwen 4 Max

  • A person is waiting on the reply and you cannot turn thinking off.
  • Repository-scale coding or long terminal-agent sessions. Alibaba's own launch comparison shows it behind higher-priced models there.
  • You need to send audio or PDF files directly. It accepts text, images and video.

The full review, with third-party benchmarks and a same-prompt comparison against three other models, is on the Qwen 4 Max review page at apimodels.app.

Top comments (0)