trim_messages() — langchain Function Reference
Architecture documentation for the trim_messages() function in utils.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD d6e12d35_63a7_f8dc_cd86_45811dae362e["trim_messages()"] 0b528c80_0ce7_1c74_8932_bc433bcb03c6["utils.py"] d6e12d35_63a7_f8dc_cd86_45811dae362e -->|defined in| 0b528c80_0ce7_1c74_8932_bc433bcb03c6 7d8fdbaf_a57f_bad7_f47e_85c3fa1f78fe["convert_to_messages()"] d6e12d35_63a7_f8dc_cd86_45811dae362e -->|calls| 7d8fdbaf_a57f_bad7_f47e_85c3fa1f78fe 83dc7aaa_e1bf_befe_66c0_fdea2b3bccdf["_first_max_tokens()"] d6e12d35_63a7_f8dc_cd86_45811dae362e -->|calls| 83dc7aaa_e1bf_befe_66c0_fdea2b3bccdf 9e133c6c_250c_1c48_07a7_cf5acf5d50d3["_last_max_tokens()"] d6e12d35_63a7_f8dc_cd86_45811dae362e -->|calls| 9e133c6c_250c_1c48_07a7_cf5acf5d50d3 style d6e12d35_63a7_f8dc_cd86_45811dae362e fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/core/langchain_core/messages/utils.py lines 1082–1468
def trim_messages(
messages: Iterable[MessageLikeRepresentation] | PromptValue,
*,
max_tokens: int,
token_counter: Callable[[list[BaseMessage]], int]
| Callable[[BaseMessage], int]
| BaseLanguageModel
| Literal["approximate"],
strategy: Literal["first", "last"] = "last",
allow_partial: bool = False,
end_on: str | type[BaseMessage] | Sequence[str | type[BaseMessage]] | None = None,
start_on: str | type[BaseMessage] | Sequence[str | type[BaseMessage]] | None = None,
include_system: bool = False,
text_splitter: Callable[[str], list[str]] | TextSplitter | None = None,
) -> list[BaseMessage]:
r"""Trim messages to be below a token count.
`trim_messages` can be used to reduce the size of a chat history to a specified
token or message count.
In either case, if passing the trimmed chat history back into a chat model
directly, the resulting chat history should usually satisfy the following
properties:
1. The resulting chat history should be valid. Most chat models expect that chat
history starts with either (1) a `HumanMessage` or (2) a `SystemMessage`
followed by a `HumanMessage`. To achieve this, set `start_on='human'`.
In addition, generally a `ToolMessage` can only appear after an `AIMessage`
that involved a tool call.
2. It includes recent messages and drops old messages in the chat history.
To achieve this set the `strategy='last'`.
3. Usually, the new chat history should include the `SystemMessage` if it
was present in the original chat history since the `SystemMessage` includes
special instructions to the chat model. The `SystemMessage` is almost always
the first message in the history if present. To achieve this set the
`include_system=True`.
!!! note
The examples below show how to configure `trim_messages` to achieve a behavior
consistent with the above properties.
Args:
messages: Sequence of Message-like objects to trim.
max_tokens: Max token count of trimmed messages.
token_counter: Function or llm for counting tokens in a `BaseMessage` or a
list of `BaseMessage`.
If a `BaseLanguageModel` is passed in then
`BaseLanguageModel.get_num_tokens_from_messages()` will be used. Set to
`len` to count the number of **messages** in the chat history.
You can also use string shortcuts for convenience:
- `'approximate'`: Uses `count_tokens_approximately` for fast, approximate
token counts.
!!! note
`count_tokens_approximately` (or the shortcut `'approximate'`) is
recommended for using `trim_messages` on the hot path, where exact token
counting is not necessary.
strategy: Strategy for trimming.
- `'first'`: Keep the first `<= n_count` tokens of the messages.
- `'last'`: Keep the last `<= n_count` tokens of the messages.
allow_partial: Whether to split a message if only part of the message can be
included.
If `strategy='last'` then the last partial contents of a message are
included. If `strategy='first'` then the first partial contents of a
message are included.
end_on: The message type to end on.
If specified then every message after the last occurrence of this type is
ignored. If `strategy='last'` then this is done before we attempt to get the
last `max_tokens`. If `strategy='first'` then this is done after we get the
first `max_tokens`. Can be specified as string names (e.g. `'system'`,
`'human'`, `'ai'`, ...) or as `BaseMessage` classes (e.g. `SystemMessage`,
`HumanMessage`, `AIMessage`, ...). Can be a single type or a list of types.
Domain
Subdomains
Defined In
Source
Frequently Asked Questions
What does trim_messages() do?
trim_messages() is a function in the langchain codebase, defined in libs/core/langchain_core/messages/utils.py.
Where is trim_messages() defined?
trim_messages() is defined in libs/core/langchain_core/messages/utils.py at line 1082.
What does trim_messages() call?
trim_messages() calls 3 function(s): _first_max_tokens, _last_max_tokens, convert_to_messages.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free