The chat history accumulates EVERY prior tool result across turns. The current code returns the full history every call. The trace shows tokens climbing turn-over-turn until the context fills with irrelevant chatter.
Fix line 8 so the assembled context only includes messages with
age <= 2 (the most recent 2 turns). Older tool_result rows
fall off the back.
Expected output:
3
The break is on line 13 — but read the whole snippet first.