Bound prompt size
A graph accumulates a long conversation and needs to keep prompts within the selected model's context window.
AI Agents · Function
trimMessages reduces a conversation history to fit an LLM's context window. Configure the retention strategy, token budget, and valid message boundaries before invoking the model.
trimMessages selects a bounded portion of a message list before an LLM call. With the last-message strategy, it retains the newest messages up to the configured token budget while the boundary options help preserve valid conversation structure.
trimMessages(messages, { strategy, maxTokens, startOn, endOn })| Name | Type | Description |
|---|---|---|
messages | BaseMessage[] | The message list to trim, such as state.messages. |
strategy | string | Controls which side of the history is retained; the documented example uses "last". |
maxTokens | number | Maximum number of tokens to retain. |
startOn | string | Message type at which the retained segment may begin. |
endOn | string[] | Message types at which the retained segment may end. |
A trimmed message list suitable for passing to the model.
import { trimMessages } from "@langchain/core/messages";
import { StateSchema, MessagesValue, GraphNode } from "@langchain/langgraph";
const State = new StateSchema({ messages: MessagesValue });
const callModel: GraphNode<typeof State> = async (state) => {
const messages = trimMessages(state.messages, {
strategy: "last",
maxTokens: 128,
startOn: "human",
endOn: ["human", "tool"],
});
const response = await model.invoke(messages);
return { messages: [response] };
};Keeps a bounded recent message history before invoking the model.
A graph accumulates a long conversation and needs to keep prompts within the selected model's context window.
A model call should use recent dialogue while preserving human and tool message boundaries.
A node receives the complete state history but should send only a budgeted subset to the LLM.
It trims the message history before invoking the model, using a token budget and a strategy such as retaining the last messages. The JavaScript example uses strategy: "last" and maxTokens: 128.
Follow-up: How can boundary options help? Set startOn and endOn to message roles that form valid boundaries, such as starting on human and ending on human or tool messages.
strategy: "last" keeps the recent portion of the conversation, while maxTokens limits the retained history. This reduces the chance that the prompt exceeds the model's context window.
Follow-up: What should be adjusted for a different model? Recalculate the budget for that model's supported context window and token-counting behavior.
The example passes startOn: "human" and endOn: ["human", "tool"]. These options constrain where trimming may begin and end, helping avoid cutting the history at unsuitable message boundaries.
Follow-up: Why might tool messages matter? A tool result can be part of the latest interaction, so retaining it may preserve the context needed for the next model call.
MessagesValueGraphNodeBaseMessagemodel.invoke