Kimi API: one standalone streaming request
ChinaAI — prepared 1 October 2026

Files
  stream_request.py: Python 3 standard library; one paid request, no retries.
  test_stream_request.py: local checks with network access blocked.
  README.txt: setup, completion rules and limits of the example.
  local-check-results.txt: the October 1 offline verification record.
  k3-stream-public-fixture-2026-09-25.json: the preserved, public, sanitized
    September 25 stream record, copied without changing it.

Run the example
1. Use a current Python 3 installation and extract these files into one folder.
2. Open a normal terminal in that folder.
3. Windows: py stream_request.py, or python stream_request.py.
   macOS/Linux: python3 stream_request.py.
4. At "Kimi API key (hidden):", enter your international Open Platform key
   and press Enter. No characters appear while entering the key. Ctrl+C
   cancels entry before a request is sent. Use a regular terminal if hidden
   input is unavailable; the example will not fall back to visible entry.

A nonempty MOONSHOT_API_KEY supplied by your secret manager takes priority.
With that environment key, running the file immediately sends one paid
request. The hidden prompt does not set a variable for later commands.
The program does not save or print the key. It does not print raw HTTP/error
payloads. Keep your key out of code, screenshots, public messages and logs.

Request and output
The fixed endpoint is https://api.moonshot.ai/v1/chat/completions. The request
uses kimi-k3, low reasoning effort, a 4,096-token completion allowance,
stream=true and stream_options.include_usage=true. This is the same payload
as the preserved September 25 streaming trial, not a new live test.
Thinking and the visible answer share the completion allowance; this is not
a sitewide spending cap. Your account must have access and API credit.

Answer fragments print as provisional text. Reasoning fragments are retained
separately in memory and are not printed. A complete text result needs a
nonempty answer, finish_reason=stop, [DONE] and no reported stream error or
pending tool call. Completion does not establish factual accuracy.

Length/tool-call finishes, cancellation, malformed events and disconnections
are not reported as successful text answers. Text received before a failure
is labelled partial. Closing the connection or timing out does not prove
that provider processing or billing stopped. No retry is sent.

The connection has a 120-second socket timeout and the reader checks a
120-second elapsed deadline before and after reads, shortening the socket
timeout when the connection's socket remains available. This is a bounded
example, not a precise guarantee about remote cancellation or billing.
DNS resolution and request/header operations do not have a strict
120-second wall-clock limit; this example is not a hard wall-clock timer.

Reported numeric prompt/completion/total usage is shown if received. Missing
usage or fields remain unknown, not zero. The program does not reconstruct
charges from partial text or call a token-count endpoint. Check provider
usage before sending another request after an uncertain result.

Local verification
Windows: py -m unittest -v test_stream_request
macOS/Linux: python3 -m unittest -v test_stream_request
These tests block socket creation and real HTTPSConnection use. They use
dummy keys and fake responses. The fixture's saved JSON chunks are framed
as SSE locally; these are parser checks, not another live network response.
All 28 checks passed on October 1; see local-check-results.txt for scope.
The original September output and its limitations remain unchanged.

Sources
https://platform.kimi.ai/docs/guide/utilize-the-streaming-output-feature-of-kimi-api
https://chinaai.chat/kimi/api/

Exit status: 0 for a completed text answer, 1 for an incomplete/error result,
130 for cancellation after sending. Key-entry cancellation exits before
sending. An unknown usage report is never treated as a zero-cost result.
