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