A tool can return far more text than an agent should read: a web page, a log file, a database dump. Pasting it all into the context wastes tokens, costs money and pushes out the instructions the model needs. The usual defense is to truncate the output to a budget. Cutting only the end loses the conclusion, and cutting only the start loses the beginning, so a good truncation keeps the head and the tail and marks the gap with a note saying how much was removed, so the model knows the text is incomplete and can ask for the missing part.
Implement truncate_to_budget.
tokens = text.split(). If len(tokens) <= max_tokens, return text unchanged.head = int(max_tokens * head_frac) tokens from the start and tail = max_tokens - head tokens from the end, and join head_tokens + [marker] + tail_tokens with single spaces, where marker = f'[... {omitted} tokens omitted ...]' and omitted = len(tokens) - max_tokens.max_tokens >= 1 and 0 <= head_frac <= 1. If tail is 0 the tail part is empty (use tokens[len(tokens):], not tokens[-0:]).tokens[-tail:] with tail = 0 returns everything, so slice from len(tokens) - tail instead.
The marker is not counted against the budget.
No results yet
Run your code to check it against the examples, or submit to run every test.