Count the Session Current Tokens
Before performing context editing, it’s essential to know the current token count of your session. This helps you understand how much context is being consumed and when you need to apply editing strategies. Theget_token_counts() method returns the total token count for all text and tool-call parts in a session, giving you visibility into your context window usage.
You can use the token count to determine if you need to edit the context.
The token count of Acontext is relative and proportional to the length of your session.
You can use it to determine whether the current session is too long and needs to be edited.Please do not use the token count to calculate the cost of LLM, as the actual token consumption of each LLM can vary subtly.
Context Editing On-the-fly
Acontext supports to edit the session context when you obtain the current messages. The basic usage is to pass theedit_strategies to the get_messages method to get the edited session messages without modifying the original session storage:
Token Limit
This strategy truncates messages based on token count, removing the oldest messages until the total token count is within the specified limit. It’s useful for managing context window limits and ensuring your session stays within model constraints. It will:- Removes messages from oldest to newest
- Maintains tool-call/tool-result pairing (when removing a tool-call, its corresponding tool-result is also removed)
Remove Tool Result
This strategy will replace the oldest tool results’ content with a placeholder text to reduce the session context, while keeping the most recent N tool results intact. Parameters:keep_recent_n_tool_results(optional, default: 3): Number of most recent tool results to keep with original contenttool_result_placeholder(optional, default: “Done”): Custom text to replace old tool results with
Context Engineering and Editing
Context Engineering is an emerging discipline focused on designing, managing, and optimizing the information provided to large language models (LLMs) and AI agents to enhance their performance, reliability, and consistency. While prompt engineering concentrates on crafting specific inputs, context engineering encompasses the entire spectrum of contextual elements that influence an AI system’s behavior and outputs.
https://blog.langchain.com/context-engineering-for-agents/