Lesson 4 of 5 · 2 min
Context engineering
Long tool results crowd the context until the agent loses sight of the goal. Keep the goal and decisions, summarise old steps, store bulky output elsewhere and fetch it when needed.
history = ["goal: fix the failing test"] + [f"log line {i}: " + "x " * 30 for i in range(8)] + ["decision: bug is in date parsing"]
def compact(items):
keep = [m for m in items if m.startswith(("goal", "decision"))]
dropped = len(items) - len(keep)
return keep[:1] + [f"summary: {dropped} log entries stored on disk"] + keep[1:]
before = sum(len(m.split()) for m in history)
after = compact(history)
print(before, "words ->", sum(len(m.split()) for m in after))
print(after)
