Build an AI SDLC Software Factory with Claude Code starts 12 December. Enrolment closes in 9 days
Courses / Cut AI Agent Costs with Token EconomicsProLesson 3 of 85:35

Cut AI agent input cost with prompt caching

How do you stop an AI agent from paying for the same input again and again? Param Harrison shows that the last turn looks small, but the whole run resends the context many times. When we code in Claude Code or Cursor, the input side is where we try to control the cost. The main tool is the cache. Keep the order of your prompt the same each turn. Put what does not change at the top and new information at the bottom. A long-running supervisor agent should read what it needs when it needs it, not keep everything in memory. After this lesson you can order your prompt so the cache does most of the work. You will also hear how ProjectDiscovery reordered its prompt so more of it was cached. And how Spotify cut what its agents read.

What this covers: input that builds up over a run, prompt caching, and context engineering. It also covers long-running agents, the static and dynamic parts of a prompt, ProjectDiscovery and Spotify.

Full session: Cut AI Agent Costs with Token Economics

#aiagents #llm #aicost

Chapters

  1. The last turn hides the whole run
  2. Keep the order so the cache works
  3. Long-running agents and memory
  4. Static on top, new at the bottom
  5. Notes at the end of the prompt

Transcript

The last turn hides the whole run

For example, when your agent is actually calling again and again in terms of agent loop, you might think, okay, the last token was just 48k tokens, but the whole run might be 580k tokens. So the last run alone won't provide the big picture in terms of how many how much of input which has been accumulated. So the input can easily accumulate and resends the whole context again and again. This is one of the reasons why most of the time when we are actually coding in Claude Code or cursor, the main place where we where we try to control the cost factor will be on the input side. Like if we are able to reduce the amount of context which has been read, or if we are able to reduce the amount of ingestion which has to be done, then we will be able to control that and we will be able to progress on that. So the input is where you need to focus, like how much input that needs to get into the loop, and we will be able to handle

Keep the order so the cache works

that. The primary focus for that input will be cache. So, in an agentic run, when you are actually setting you know the top level parameters, you need to make sure the sequence is well maintained so that the consecutive turns is not going geometrically increasing, rather, you will be utilizing your cache as much, and then the amount of you know the cache will take care of most of them and remaining remaining newer input is what it will be sent again to the prompt. This is where many of the techniques has been utilized in terms of context management and context engineering is a whole new like thing in terms of agentic building where you will be managing your context, what to summarize and how to cache those summaries per turn.

Long-running agents and memory

So, in a long-running agent, for example, your agent is running whole night, or you will be creating a coding agent that will be creating your PR, and that is a huge epic, and this agent is supervising and merging all these PRs. Now, this supervising agent can able to read what is going on, ad hoc rather than it will keep everything in the memory. Otherwise, what happens is like it will accumulate and accumulate and accumulate, then two problems will arise. One is you will exceed the context memory, and then the hallucination will start. So you need to make sure you need to smartly compact and you need to utilize the cache. How to do that?

Static on top, new at the bottom

The better approach is always what will go in the top that need to be static, and what will go in the bottom that need to be dynamic. So anything that newer information needs to go in the bottom, and anything that is older information that needs to go in the top, for example, when you are sending a user related data along with you know system prompt and then your prompt and then user related context. In this case, always put your system prompt, which is static, and then the user query, which is going to be static, and then like they they they send once, and then whatever information you are retrieving, you need to selectively select what are all the things which can go stale and what are all the things which will be across this whole session. Then you need to pick and choose what will go into context memory and what will be fetched out of memory. So that way you are basically creating the cache. This is my cache, and keep this in reading cache in next agentic run. So next time when a when a sub agent is called and it is actually bringing you the you the information, it is not going to call the entire thing as fresh fresh data, rather it will be pulling that as okay. This agent already have this as cache, and only this information is additional information for you, and it will operate in the agentic loop.

Notes at the end of the prompt

Okay, and ProjectDiscovery, a company which kept the agent's note near the top, only 7% of reads were cheap. They moved the notes to the end, cheap you know, reads rose to 84%. So, this is what for example, last week Spotify have mentioned, like instead of you know in their coding agent, what they did was a smart input caching, like instead of reading every time the agent to read the code base again and again without having the state, they will build a graph of the code base changes, and based on that, they will provide like what to read specifically for that part of code.

This lesson comes with Pro, $149 a year: Get Pro. This lesson is cut from the free Lightning session Cut AI Agent Costs with Token Economics. The Live Labs build the same system with you, free, on Thursdays: see the Live Labs.