Skip to content

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

@donMichaelL

Description

@donMichaelL

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    langchainFor docs changes to LangChain

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions