- Upload and read: get model-readable text with the file-extract flow;
- Ask: put the file content into messages as a system message;
- Multi-turn chat: keep the fixed prefix in place and append turns to the end of messages;
- Multi-file Q&A: put each file in its own system message at the head of messages;
- Constrain the output: add a fixed response rule to control format and length;
- Cut costs: reuse a stable document prefix so Context Caching kicks in automatically;
- Clean up uploaded files: keep the extracted text locally and delete uploaded files on a schedule.
1. Setup
The Kimi API is compatible with the OpenAI SDK, so you only need the openai package:Example output
Example output
2. Upload the file
To get started, we’ll upload the PDF through the Files API with purpose set to file-extract, which marks the file for text extraction. The Files API also accepts purpose values such as image and video, used for the model’s native understanding of those files:Example output
Example output
3. Read the extracted content
After uploading, fetch the extracted text through the file content endpoint. It already follows the format that the official documentation recommends for model consumption:Example output
Example output
Two common mistakes:
- Do not put the file_id into messages. The file_id is only a handle; the model cannot see any content through it. You must read the extracted content first and put the text into messages.
- Do not inline a base64-encoded PDF into messages. Base64-encoded files lead to very high token consumption. If the file type is supported by the /v1/files API, upload the file and extract its content instead.
4. Ask a question
Put the extracted file content into messages as a system message, then ask your question in a user message:Example output
Example output
Note: all outputs in this cookbook come from real runs. Model output is not deterministic, so your results may differ slightly.
5. Multi-turn chat
To follow up, append the question and answer to the end of messages. The file content and instructions stay at the front, unchanged. This “fixed content first, conversation appended at the end” structure is not just a convention; it determines whether the cache described later can be hit:Example output
Example output
6. Multi-file Q&A
Multi-file Q&A is the same pattern scaled out: put each file in its own system message and place these messages at the head of the messages list:Example output
Example output
Example output
Example output
7. Constrain the response
The system message in section 4 is an identity setting: it tells the model who it is. In practice you often add a response rule that tells the model what the answer should look like. Format, length, and style can all be pinned down with a fixed instruction. The rule is part of the stable prefix, just like the identity setting and the file content, and does not affect cache hits in the next section:Example output
Example output
Example output
Example output
8. Cut costs: Context Caching
Billing for file Q&A has a structural property: the document content appears in every request as a fixed prefix, so the more questions you ask about the same document, the more times that prefix is billed. Context Caching removes this repeated cost. The Kimi API enables it automatically for all requests: when the system detects a repeated leading context (system prompt, file content, tool definitions, and so on), it reuses the cached prefix and bills it as a cache hit instead of charging the full price again. No extra code is needed. You do not create a cache, reference a cache ID, or manage a TTL. Just call /v1/chat/completions as usual. There is only one thing to do: keep the fixed parts (file content, system prompt, tool definitions) stable and at the front of the messages array. Two details from the official documentation:- A later request can only hit the prefix cache when the preceding request has more than 256 prompt tokens; smaller requests are not cached. File Q&A meets this condition naturally.
- For reference, the official figures are up to 90% cost reduction in specific scenarios, and average time to first token under 5 seconds for long texts. Check the pricing page for the exact billing rules.
Example output
Example output
9. Clean up uploaded files
The Files API limits each user to 1,000 files and 10GB in total, so delete uploaded files once you are done with them. For a periodic full cleanup, list all files with files.list and delete them one by one with files.delete:Example output
Example output
10. FAQ
What if extraction fails
What if extraction fails
Extraction can fail for several reasons: unsupported format, corrupted file, or exceeding the 100MB limit. Formats the API does not support cannot be parsed by the model, so do not put them into the context. Wrapping upload and extraction in defensive handling is a good idea:
Example output
What if the document is too long
What if the document is too long
With kimi-k3’s 1M-token context, most single documents fit in one piece. If a document is really too long, split it along its own structure (chapters, headings) and put each part in a separate system message.If your scenario is “a huge document collection with unpredictable queries”, for example asking anything across an entire knowledge base, that is beyond what single-document Q&A covers. See the Context Caching versus RAG guidance in section 8.
Reference docs: File-based Q&A with the Kimi API, File upload API reference, Context Caching with the Kimi API.