[GH-ISSUE #1179] [langchain]: Improve clarity and examples in “Trim Messages” section of short-term memory documentation #162

Open
opened 2026-02-17 17:19:18 -05:00 by yindo · 0 comments
Owner

Originally created by @donMichaelL on GitHub (Oct 29, 2025).
Original GitHub issue: https://github.com/langchain-ai/docs/issues/1179

Type of issue

issue / bug

Language

Python

Description

The current Trim Messages section of the documentation is less clear compared to the previous version.

  • The explanation of the trim message mechanism is somewhat hard to follow, and it’s not immediately clear how to use it effectively.
  • The example focuses mainly on manually deleting messages, without referencing or illustrating the strategy mentioned earlier in the description.
  • Additionally, the trim_message function does not appear to be deprecated in v1.0, so it likely remains the correct function to reference and demonstrate in this section.
@before_model
def trim_messages(state: AgentState, runtime: Runtime) -> dict[str, Any] | None:
    """Keep only the last few messages to fit context window."""
    messages = state["messages"]

    trimmed_messages = trim_messages(
        messages,
        max_tokens=45,
        strategy="last",
        token_counter=ChatOpenAI(model="gpt-4o"),
        start_on="human",
        include_system=True,
        allow_partial=False,
    )

    return trimmed_messages
Originally created by @donMichaelL on GitHub (Oct 29, 2025). Original GitHub issue: https://github.com/langchain-ai/docs/issues/1179 ### Type of issue issue / bug ### Language Python ### Description The current [Trim Messages](https://docs.langchain.com/oss/python/langchain/short-term-memory#trim-messages) section of the documentation is less clear compared to the previous version. - The explanation of the trim message mechanism is somewhat hard to follow, and it’s not immediately clear how to use it effectively. - The example focuses mainly on manually deleting messages, without referencing or illustrating the strategy mentioned earlier in the description. - Additionally, the [trim_message](https://reference.langchain.com/python/langchain/messages/?h=trim#langchain.messages.NonStandardContentBlock.id) function does not appear to be deprecated in v1.0, so it likely remains the correct function to reference and demonstrate in this section. ```python @before_model def trim_messages(state: AgentState, runtime: Runtime) -> dict[str, Any] | None: """Keep only the last few messages to fit context window.""" messages = state["messages"] trimmed_messages = trim_messages( messages, max_tokens=45, strategy="last", token_counter=ChatOpenAI(model="gpt-4o"), start_on="human", include_system=True, allow_partial=False, ) return trimmed_messages ```
yindo added the langchainexternal labels 2026-02-17 17:19:18 -05:00
yindo changed title from [langchain]: Improve clarity and examples in “Trim Messages” section of short-term memory documentation to [GH-ISSUE #1179] [langchain]: Improve clarity and examples in “Trim Messages” section of short-term memory documentation 2026-06-05 17:25:23 -04:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/docs#162