Coders In Hoods
  • Courses
  • Roles
  • Blog
  • Setup

Coders In Hoods AI Academy

Every course is free. Sign in to start, and your progress, quiz scores and bookmarks follow you.

AI Academy

  • Courses
  • Paths
  • Setup guides
  • How we teach

Role roadmaps

  • AI Engineer
  • Forward Deployed Engineer
  • Machine Learning Engineer
  • All roles

Coders In Hoods

  • The agency
  • Coaching for teams
  • Blog
  • LinkedIn
  • X (Twitter)

Contact

codersinhoods@gmail.com

© Coders In Hoods · since 2019

Course overview

LLM Cost Engineering

Free · sign in to start and track progress

1 · Spending less on the same work

  • 1Token economics of an agent fleet
  • 2Prompt caching as system design
  • 3Trimming and compacting context

    Exercise answers

    • Answer: Simulate context policies with and without caching
    • Answer: Test what compaction forgets
  • 4Batch, flex and fast tiers

2 · Changing the model, and governing the spend

  • 5Cost-quality curves, routing and cascades
  • 6Distilling into small models
  • 7Self-hosting break-even
  • 8Attribution, budgets and alerts
CapstoneCost-engineer an agent pipelineReference implementation

Module 1 · Spending less on the same work · Lesson 3

Trimming and compacting context

The four ways to send fewer tokens (select, clear, compact, compress), how each one interacts with the prompt cache, and the break-even arithmetic that tells you when trimming saves money and when it costs more.

70 min 1 video 3 exercises 6-question quiz Answers
Setup for the exercises:Install Claude CodePython with uv

By the end of this lesson you can

  • Distinguish selecting, clearing, compacting and compressing context, and name the cost and quality risk of each
  • Derive how many further steps a context edit needs before it pays back under caching, and apply it to tool-result clearing
  • Configure tool-result clearing and server-side compaction on the Claude API, and account for compaction in usage tracking
  • Compare trimming policies for a long-running agent with and without caching, and explain why aggressive trimming can raise cost

On this page

  • Intuition: clearing the desk
  • Four ways to send fewer tokens
  • The mechanism: what an edit costs under caching
  • A worked example: a 40-step matter assistant
  • Selection, in numbers
  • Configuring clearing and compaction
  • Misconceptions and limits
  • Connections
  • Summary
  • Sources & further reading
  • Quiz

In this lesson

  1. 01Intuition: clearing the desk
  2. 02Four ways to send fewer tokens
  3. 03The mechanism: what an edit costs under caching
  4. 04A worked example: a 40-step matter assistant
  5. 05Configuring clearing and compaction
  6. 06Misconceptions and limits
  7. 07Connections
  8. 08Summary

Sources & further reading

  • ACON: Optimizing Context Compression for Long-horizon LLM AgentsPaper · Kang, Chen, Han, Inan, Wutschitz, Chen, Sim, Rajmohan · 2025Learns compression guidelines for agent history and observations from failure analysis, cutting peak tokens by 26–54% while improving task success. Read section 3 for the guideline-optimisation loop and the distilled compressor.
  • LLMLingua: Compressing Prompts for Accelerated Inference of Large Language ModelsPaper · Jiang, Wu, Lin, Yang, Qiu · Microsoft Research · 2023Token-level prompt compression with a small model, up to 20× with little loss on its benchmarks. The reference point for compress-before-send approaches and their limits on exact legal text.
  • Lost in the Middle: How Language Models Use Long ContextsPaper · Liu et al. · 2023The quality side of the argument. Long, cluttered contexts are used unevenly, so trimming can raise accuracy as well as lower cost.
  • Effective context engineering for AI agentsLab research · AnthropicThe case for treating context as a finite resource, with compaction, structured note-taking and sub-agents as the main techniques for long-horizon work.
  • Context editing (Claude API docs)Docs · AnthropicTool-result clearing and thinking-block clearing, with trigger, keep, clear_at_least and exclude_tools, and the explicit warning that clearing invalidates the cached prefix.
  • Compaction at a token threshold (Claude API docs)Docs · AnthropicServer-side compaction, its 150,000-token default trigger and 50,000 minimum, caching the system prompt separately, and the usage.iterations rule for counting compaction tokens.
Free · sign in to start

Sign in to keep reading.

Free, no card. We use your account to save progress, quiz scores, bookmarks and occasionally send updates from our team.

  • 8 more sections
  • 3 hands-on exercises
  • A 6-question quiz
  • Progress, quiz scores and bookmarks