The examples on this page use the latest model
kimi-k3 by default. K3 configures reasoning effort with the top-level reasoning_effort request field (supports "low" / "high" / "max", default "max"). To use another model such as kimi-k2.6 or kimi-k2.5, just replace the model field — parameter configurations differ across models. See the Model Parameter Reference.Give the model memory with the messages list
The following example modifies the one from the previous chapter to show how maintaining amessages list gives the model memory: each turn appends both the user’s new message (role=user) and the model’s reply (role=assistant) to the list, then sends the whole list with the request. The key points are annotated as comments in the code:
- python
- node.js
- The Kimi API has no built-in context memory; use the
messagesparameter to manually tell the model what has been discussed before; - The
messageslist must store both the user’s questions (role=user) and the model’s replies (role=assistant).
Truncate history to control context length
As the number ofchat calls grows, the messages list keeps getting longer, so each request consumes more Tokens—eventually the messages in the list will exceed the context window supported by the model. Use a strategy to keep the messages list within a manageable range, for example by keeping only the latest 20 messages as the context for each request.
The following example shows how the make_messages function controls the number of messages in each request (keeping the latest 20 by default)—note how it ensures the System Messages remain in the list even when truncating:
- python
- node.js
What else to consider in production
The examples above only cover the simplest invocation scenario. In real business logic, you may need to handle more scenarios and edge cases:- In concurrent scenarios, additional read-write locks may be needed;
- For multi-user scenarios, maintain a separate
messageslist for each user; - Persist the
messageslist; - Use a more precise way to determine how many messages to retain in the
messageslist; - Summarize the discarded messages and add the summary as a new message to the
messageslist; - ……