Videos uiP88SpCi1Q
Your Agent Is Wasting Tokens and You Don't Know It - Erik Hanchett, AWS
Scene timeline
16 shot(s).
keyframes kept every frame deduplicated
What was stored
- cues
- 63
- whisperx 63
- chunks
- 10
- from 63 cues
- keyframes
- 15
- kept of 16 captured
- frames with text
- 15
- 95 lines read
- chapters
- 0
- from the source metadata
- keyframe bytes
- 1.5 MB
- word timings on 63 cues
Provenance
| stage | state | model | started | took |
|---|---|---|---|---|
fetch |
done | — | 2026-08-11 05:59 | 1m 21s |
stt |
done | — | 2026-08-11 06:00 | 6s |
chunk |
done | — | 2026-08-11 06:00 | 0s |
text_embed |
done | — | 2026-08-11 06:00 | 1s |
keyframe |
done | — | 2026-08-11 06:00 | 19s |
ocr |
done | — | 2026-08-11 06:00 | 3s |
frame_embed |
done | — | 2026-08-11 06:00 | 3s |
Frames, and what the machine read
-
- Your Agent Is1.00
- Wasting1.00
- Tokens1.00
- Cutting Al Agent Token Costs0.98
- Erik Hanchett1.00
- Developer Advocate at AWS1.00
-
- Costing Too0.99
- 3X0.86
- Much1.00
-
- Cache System1.00
- Prompt1.00
-
- FIX 010.93
- Cache the System Prompt1.00
- agent = Agent(0.99
- model=BedrockModel(1.00
- model_id="claude-sonnet",1.00
- cache_prompt="default",1.00
- cache static prefix1.00
- ),0.75
- system_prompt=BIG_SYSTEM_PROMPT,1.00
- U50.73
-
- Route By1.00
- Difficulty0.99
-
- FIX 021.00
- Route by Difficulty1.00
- def pick_model(task):1.00
- if task.is_simple:1.00
- return "claude-haiku"1.00
- #1.00
- cheap1.00
- return "claude-sonnet"1.00
- expensive reasoning1.00
-
- Offload Tool1.00
- Results1.00
-
- FIX 031.00
- Offload Big Tool Results0.98
- @tool1.00
- def fetch_report(id: str) → str:0.99
- data = api.get(id)1.00
- # 10k tokens0.98
- key = store.put(data)1.00
- # offload0.97
- return summarize(data, key)0.99
- # summary + ref1.00
- *only the summarized tool result gets added1.00
- not the full tool result1.00
-
- Cap Your Tool0.98
- Loops1.00
-
- FIX 040.98
- Cap Your Tool Loops1.00
- agent = Agent(1.00
- tools=[...],0.99
- max_iterations=8,1.00
- # stop runaway loops0.99
-
- Trim the History1.00
-
- FIX 050.99
- Trim the History0.99
- from strands.agent.conversation_manager import (0.99
- SlidingWindowConversationManager,0.99
- agent = Agent(0.98
- conversation_manager=SlidingWindowConversationManager(1.00
- window_size=10,1.00
- *multi-turn conversation agent1.00
-
- FIX 050.99
- Trim the History0.99
- from strands.agent.conversation_manager import (0.99
- SlidingWindowConversationManager,1.00
- agent = Agent(0.96
- conversation_manager=SlidingWindowConversationManager(1.00
- window_size=10,1.00
-
- RECAP1.00
- // FIVE FIXES1.00
- Same Agent. Smaller Bill.1.00
- 011.00
- Cache the System Prompt1.00
- stop resending the static prompt every turn1.00
- 021.00
- Route by Difficulty1.00
- cheap model for simple calls1.00
- 031.00
- Offload Big Tool Results1.00
- keep 10k-token blobs out of context1.00
- 041.00
- Cap Your Tool Loops0.99
- max_iterations, no runaways1.00
- 051.00
- Trim the History1.00
- window or summarize old turns1.00
-
- Thanks!1.00
- Go Deeper0.99
- Erik Hanchett0.97
- Social Media1.00
- ErikCH1.00
- Site1.00
- programwitherik.com1.00
Transcript
63 cues· 991 words· 5,160 chars
- 0:00 Hey everyone.
- 0:01 My name is Eric Hanchett.
- 0:02 I am a senior developer advocate at AWS, and I'm going to talk to you about how you can save on token costs.
- 0:09 Now I'm going to show you five ways that you can reduce your token costs while using and creating agents.
- 0:18 So the first way you can do that is to cache your system prompt.
- 0:24 Let me show you some code.
- 0:25 Now I'm using AWS's strands agents.
- 0:29 This works with all different providers.
- 0:33 This is a little bit of pseudo code, but the idea is that you can add cache prompt equals default.
- 0:39 And what that'll do is on the first call of your agent,
- 0:43 it will send the full system prompt over.
- 0:47 And then on every subsequent call, it will have a much reduced system prompt being sent over.
- 0:54 So it'll be cached.
- 0:55 You can also cache the tool prompts and messages as well.
- 1:01 This may sound obvious, but you want to look into routing your different messages based on the difficulty.
- 1:09 So here's a code example.
- 1:11 Let's imagine that you have a task that's very difficult.
- 1:15 You may want to use one of the newer Frontier models.
- 1:18 However, if it's something simpler, you want to use a cheaper model.
- 1:23 In this case, maybe we use Cloud Haiku for cheap.
- 1:26 something cheap and then use cloud sonnet for something a little bit more difficult and then you can use an if statement you can even have another model that's very cheap decide which model to use so you can play around with this but i highly recommend don't use the most expensive model for everything you're doing you want to use multiple different models based on the use case and then try to route to it inside your agent
- 1:51 Another good tip is to offload the tool result.
- 1:55 Let me show you some code on here.
- 1:56 Once again, I'm using strands agents.
- 1:58 This is a manual way to do it.
- 2:00 There is some additional APIs that the strands agents offers to do this.
- 2:06 If you have a large tool result that's coming back, you can store it locally or in the cloud and then use some kind of summarization that saves on tokens.
- 2:18 So that way, when it's being called over and over again, the tool result isn't added into the context every time the tool loops or every time the agent loops.
- 2:30 so if you can find any way that where you have this tool result that you don't necessarily send it on every single call back to the large language model that will save a lot of tokens for you and like i said there's a few apis to do this but essentially you can do this summarization technique
- 2:47 You can also cap your tool loops.
- 2:50 So when you're dealing with the agent loop and it decides to do a tool call, I've had this happen often where it calls the tool over and over and over again.
- 3:00 And if you don't cap that tool call, then it might run 10, 20 times.
- 3:06 It might get into an infinite loop, which would be very bad for your token usage.
- 3:10 So always set a max iterations of how many times it will loop.
- 3:16 A good thing you can do before you deploy your agent is to run some observability tools and take a look at the tool call use for every single tool and then see how long each one of them is running and how many times they're looping.
- 3:31 And that way you can get an idea of how efficient the tool call is.
- 3:37 Last but not least, we can trim the history.
- 3:42 So if we're using a multi-turn agent and we are talking back and forth, you will find at times that the conversation history will get very large.
- 3:52 On every single call, that whole conversation history will be sent back to the large language model.
- 3:59 And this can eat through hundreds, if not thousands, of tokens.
- 4:03 In strands agents, we have something called sliding window conversation manager, which this does is it looks back at the last 10 messages and only sends those back.
- 4:14 And you can set this to whatever you want.
- 4:16 And that way you're not sending these huge message histories back to the agent every single time a new message comes in.
- 4:25 The downfall of this, or the trade-off of this, I should say, is that you will lose the message history from the beginning.
- 4:32 The way you want to deal with that is you can use some sort of summarization of the history and then put that into the context window.
- 4:40 So rather than sending all of it, you may send a small amount once you hit this sliding window.
- 4:47 This will save you a lot of tokens.
- 4:50 So in conclusion, we have five things.
- 4:53 Cache the system prompt.
- 4:56 And if you can, maybe the tool prompt to messages.
loading