Long conversations or dense tool outputs blow through the context window. The model forgets the goal, repeats itself, and costs spike because every turn rereads everything.
Long sessions, agents that read lots of files, research tasks that keep exploring instead of converging.
Short stateless calls. The summarisation step is overhead with no benefit when the context is already small.
Describe this pattern to the builder assistant and it wires it up for you, brains, tools and triggers. It's vibe coding for agents, no config to write.
Walking through the pattern one piece at a time so the design is clear, not memorised.
The hook fires at 70% of the model's context window, not at a fixed turn number. Long turns trigger it sooner, short turns later.
It's the same auto-compaction mechanism the runtime uses by default - this hook just moves the trigger point earlier, before pressure gets severe.
memory.set_goal on the first user message keeps the objective pinned. Even after older turns are summarised away, the model still sees what it's working toward.
Compaction runs only when pressure crosses the threshold, not on every turn - most sessions never trigger it at all.
The pattern above is not the only answer. Here is when something else is the right call.
Drop oldest turns silently. Cheap, lossy, can lose critical context the model needed.
Push facts into a rag knowledge base and recall them with rag.query on demand. Robust for very long-running agents, more setup, slower per-turn.
Engineering notes from the Digitorn team. No marketing, no launch announcements, no "10 prompts that will change your life". Just the things we write that we'd want to read.