الدرس 6 · الوحدة 1: System Prompts That Control Agents and Cut Cost
Cutting cost: shorter prompts, smaller models and prompt caching
- Trim the system prompt
- Pick a smaller model
- Use prompt caching
Why this matters
A working agent can quietly become expensive. Every run sends the system prompt, the customer message and the reply, and each part is billed by the token. A bakery's support agent that handles 9,000 messages a month can cost about 85 dollars, and that bill grows with every message. This lesson shows three levers that cut the cost of each run, and how to check that a cheaper agent still does its job.
Key ideas
- Shorter prompts. Delete repeated rules and text the agent never uses, but keep the safety rules. Fewer input tokens means a lower cost on every run. Example: a 2,000-token system prompt cut to 800 tokens, with a 150-token customer message, gives 950 input tokens at 3 dollars per million, which is 0.00285 dollars for the input.
- Smaller models. A cheaper model has a lower price per million tokens, but it may fail on hard messages, so you must test it first.
- Prompt caching. The provider keeps a stored copy of an identical starting text, so a cached read costs about a tenth of the normal input price. The cache only matches from the very start of the prompt, so the changing text goes last.
- Stack the levers. Each lever cuts the bill alone, and the three together cut it most. In the bakery example, the bill falls from about 85 dollars to about 9 dollars for 9,000 messages.
Try it before you watch
If an agent's system prompt is 1,000 tokens, the customer message is 100 tokens and the reply is 100 tokens, how many input tokens does one run send?
Show answerHide answer
1,100 input tokens (1,000 from the system prompt plus 100 from the message). Only input tokens are added together for this count, because the reply is output, and it shows that a shorter prompt cuts the input part of every run.
Words to know
- Token: a small piece of text, roughly three quarters of an English word, that providers use to measure and charge for usage.
- Input tokens: everything you send to the model, including the system prompt and the customer message.
- Output tokens: everything the model writes back, which is usually priced higher than input.
- Prompt caching: a provider feature that stores a copy of a fixed starting text so repeated runs read it at a lower price.
- Cache hit: a request whose beginning matches the stored copy, so the cheaper cached price applies.
- Tricky messages: a set of hard test cases, such as a double booking or a refund request, used to check whether a cheaper version still works.
What to watch for
- Watch how the bill moves from 85 dollars to about 9 dollars, and notice which lever cuts the most on its own.
- Notice the CHECK question about where the changing customer message goes, and why the cache needs it at the end.
- Watch the mistake slide: a date at the top of the prompt makes every token pay full price, so look for how the fix moves changing text to the end.
تعلّمه مع معلمك على السبورة المباشرة
أهم نقاط الفيديو
A bill for a month of messages
- A small bakery with an AI agent
- 9,000 messages
- About 85 dollars that month
layout: side
objects:
shop: {text: "Your bakery's support agent", pos: [0, 2.9]}
t1: {text: "Customer messages", pos: [-1.5, 2.1]}
t2: {text: "Pickup times", pos: [-1.5, 1.5]}
t3: {text: "Leads for custom cakes", pos: [-1.5, 0.9]}
t4: {text: "Invoices", pos: [-1.5, 0.3]}
t5: {text: "Sales reports", pos: [-1.5, -0.3]}
bill: {text: "The first monthly bill", pos: [0, -1.1]}
msgs: {text: "9,000 messages", pos: [0, -1.7]}
calc: {tex: '9000 \times 0.00945 = 85.05', pos: [0, -2.4]}
goal: {text: "Cheaper runs, same quality", pos: [0, -3.1]}
steps:
- at: "small bakery"
show: shop
- at: "customer messages"
show: t1
- at: "pickup times"
show: t2
- at: "qualifies leads"
show: t3
- at: "sends invoices"
show: t4
- at: "reports sales"
show: t5
- at: "monthly bill"
show: bill
- at: "9,000"
show: msgs
- at: "about 85 dollars"
show: calc
- at: "grows with every"
highlight: calc
- at: "make each run cheaper"
show: goal What you will learn
- Trim the system prompt
- Pick a smaller model
- Use prompt caching
layout: wide
parts:
llm: {chip: LLM, sub: "Priced per token"}
prompt: {box: PROMPT, holds: [llm], icon: file-text}
app: {window: "Your n8n or Make flow", holds: [prompt]}
steps:
- at: "three things"
highlight: llm
- at: "trim the system prompt"
zoom_out: prompt
spokes: {of: prompt, items: [Shorter text, Business rules, Prompt caching]}
- at: "pick a smaller model"
spokes: {of: llm, items: [Smaller model, Price per token]}
- at: "prompt caching"
highlight: prompt
- at: "different layer"
zoom_out: app Where the money goes
- Input: everything you send
- Output: everything it writes back
- Cost: under one cent per run
layout: side
objects:
tok: {text: "1 token is about 3/4 of a word", pos: [0, 3.2]}
head: {text: "One normal run", pos: [0, 2.5]}
sys: {text: "System prompt: 2,000 tokens", pos: [0, 1.8]}
msg: {text: "Customer message: 150 tokens", pos: [0, 1.2]}
rep: {text: "Reply: 200 tokens", pos: [0, 0.6]}
eq1: {tex: '2150 \times \frac{3}{1000000} = 0.00645', pos: [0, -0.3]}
eq2: {tex: '200 \times \frac{15}{1000000} = 0.003', pos: [0, -1.1]}
eq3: {tex: '0.00645 + 0.003 = 0.00945', pos: [0, -2.2]}
steps:
- at: "three quarters"
show: tok
- at: "one normal run"
show: head
- at: "system prompt is"
show: sys
- at: "customer message is"
show: msg
- at: "reply is 200"
show: rep
- at: "input is 2,150"
show: eq1
- at: "output is 200"
show: eq2
- at: "add the two together"
show: eq3
- at: "under one cent"
highlight: eq3 Lever one: a shorter prompt
- Cut repeated rules
- Keep the safety rules
- 800 tokens, not 2,000
layout: side
objects:
old: {text: "System prompt before: 2,000", pos: [0, 2.6]}
new: {text: "System prompt after: 800", pos: [0, 1.9]}
inp: {tex: '800 + 150 = 950', pos: [0, 1.0]}
c1: {tex: '950 \times \frac{3}{1000000} = 0.00285', pos: [0, 0.0]}
c2: {tex: '0.00285 + 0.003 = 0.00585', pos: [0, -1.1]}
per: {text: "Per 1,000 runs: 5.85 dollars", pos: [0, -2.2]}
find: {text: "Find text the agent never needs", pos: [3.2, 2.6]}
keep: {text: "Keep the rules that stop mistakes", pos: [3.2, 1.9]}
steps:
- at: "shorten a prompt"
show: old
- at: "finding text"
show: find
- at: "repeated rules"
highlight: old
- at: "keep the safety rules"
show: keep
- at: "drops from 2,000"
show: new
- at: "800 plus 150"
show: inp
- at: "950 input tokens"
show: c1
- at: "cost falls"
show: c2
- at: "per thousand runs"
show: per Lever two: a smaller model
- Smaller model, lower price
- Test on tricky messages
layout: side
objects:
tiny: {text: "Smaller model, per million tokens", pos: [0, 2.8]}
price: {text: "Input 0.80 dollars, output 4 dollars", pos: [0, 2.1]}
ci: {tex: '2150 \times \frac{0.80}{1000000} = 0.00172', pos: [0, 1.0]}
co: {tex: '200 \times \frac{4}{1000000} = 0.0008', pos: [0, 0.0]}
ct: {tex: '0.00172 + 0.0008 = 0.00252', pos: [0, -1.1]}
per: {text: "Per 1,000 runs: 2.52 dollars", pos: [0, -2.1]}
pct: {text: "About 73 percent cheaper", pos: [3.2, -0.5]}
tricky: {text: "Test it before you switch", pos: [3.2, -1.6]}
steps:
- at: "cheaper model"
show: tiny
- at: "costs 0.80 dollars"
show: price
- at: "2,150 input tokens"
show: ci
- at: "input part costs"
highlight: ci
- at: "output part costs"
show: co
- at: "total is 2.52"
show: ct
- at: "per thousand runs"
show: per
- at: "roughly 73 percent"
show: pct
- at: "tricky message"
show: tricky Lever three: prompt caching
- Same beginning, every run
- A cached read costs a tenth
- Changing text goes last
layout: side
direction: LR
components:
user: {kind: user, label: Customer}
app: {kind: client, label: Booking agent}
api: {kind: api, label: Model API}
cache: {kind: cache, label: Prompt cache}
llm: {kind: llm, label: Model}
links: [user->app, app->api, api->cache, api->llm]
steps:
- at: "surprising shortcut"
show: [user, app]
- at: "fixed beginning ready"
show: all
- at: "next request starts"
flow: ["user>app: new message", "app>api: same start", "api>cache: prefix hit", "cache>api: stored copy"]
- at: "a tenth of the normal"
note: "Cached read: one tenth of the price"
- at: "Only the new part"
flow: ["api>llm: 150 new", "llm>api: reply"]
- at: "must go last"
highlight: cache The caching arithmetic
- Cached part: 2,000 tokens
- 150 new tokens: full price
- Total: about 4.05 per thousand runs
layout: side
objects:
c1: {tex: '2000 \times \frac{0.30}{1000000} = 0.0006', pos: [0, 2.2]}
c2: {tex: '150 \times \frac{3}{1000000} = 0.00045', pos: [0, 1.1]}
c3: {tex: '200 \times \frac{15}{1000000} = 0.003', pos: [0, 0.0]}
sum: {tex: '0.0006 + 0.00045 + 0.003 = 0.00405', pos: [0, -1.2]}
per: {text: "Per 1,000 runs: 4.05 dollars", pos: [0, -2.3]}
on: {text: "Caching on, same run", pos: [3.4, 2.2]}
orig: {text: "Original 2,000 token prompt", pos: [3.4, 1.1]}
first: {text: "The first call costs a little more", pos: [3.4, -2.3]}
steps:
- at: "numbers change"
show: on
- at: "caching turned on"
show: orig
- at: "2,000 cached tokens"
show: c1
- at: "full 3 dollars per million"
show: c2
- at: "output is unchanged"
show: c3
- at: "Add the three parts"
show: sum
- at: "per thousand runs"
show: per
- at: "writes the cache"
show: first Stacking the three levers
- Each lever alone
- Stack all three
- Bill falls from 85 to 9
layout: side
objects:
t: {text: "Dollars a month, 9,000 messages", pos: [0, 2.9]}
b0: {polygon: [[-2.8, -2.0], [-2.0, -2.0], [-2.0, 1.4], [-2.8, 1.4]]}
b1: {polygon: [[-1.6, -2.0], [-0.8, -2.0], [-0.8, 0.11], [-1.6, 0.11]]}
b2: {polygon: [[-0.4, -2.0], [0.4, -2.0], [0.4, -1.09], [-0.4, -1.09]]}
b3: {polygon: [[0.8, -2.0], [1.6, -2.0], [1.6, -0.54], [0.8, -0.54]]}
b4: {polygon: [[2.0, -2.0], [2.8, -2.0], [2.8, -1.65], [2.0, -1.65]]}
v0: {text: "85.05", pos: [-2.4, 1.8]}
v1: {text: "52.65", pos: [-1.2, 0.4]}
v2: {text: "22.68", pos: [0, -0.8]}
v3: {text: "36.45", pos: [1.2, -0.2]}
v4: {text: "8.86", pos: [2.4, -1.4]}
k0: {text: "Now", pos: [-2.4, -2.5]}
k1: {text: "Prompt alone", pos: [-1.2, -2.5]}
k2: {text: "Model alone", pos: [0, -2.5]}
k3: {text: "Cache alone", pos: [1.2, -2.5]}
k4: {text: "All three", pos: [2.4, -2.5]}
stack: {text: "Stacked: all three levers", pos: [1.8, 2.3]}
cprice: {text: "Cached token: 0.08 per million", pos: [1.8, 1.6]}
parts: {tex: '0.000064 + 0.00012 + 0.0008 = 0.000984', pos: [0, -3.1]}
together: {text: "All three together", pos: [1.8, 0.9]}
steps:
- at: "compare the levers"
show: [t, b0, v0, k0]
- at: "shorter prompt brings"
show: [b1, v1, k1]
- at: "smaller model to"
show: [b2, v2, k2]
- at: "caching to about 36"
show: [b3, v3, k3]
- at: "Now stack all three"
show: stack
- at: "cache those 800"
show: [b4, v4, k4]
- at: "a tenth of 0.80"
show: cprice
- at: "run totals about"
show: parts
- at: "falls from about 85"
highlight: b0
- at: "to about 9 dollars"
highlight: b4
- at: "biggest drop"
show: together Test before you cut
- Ten tricky messages
- Count the answers
- One wrong answer costs more
layout: side
objects:
q: {text: "10 tricky messages", pos: [0, 2.8]}
big: {text: "Big model: 10 out of 10", pos: [0, 1.8]}
small: {text: "Smaller model: 8 out of 10", pos: [0, 0.9]}
frac: {tex: '\frac{8}{10} = 0.8', pos: [0, 0.0]}
risk: {text: "One wrong refund answer", pos: [0, -1.1]}
saved: {text: "Saves 62 dollars a month", pos: [0, -2.0]}
steps:
- at: "hard cases"
show: q
- at: "ten tricky messages"
highlight: q
- at: "count the answers"
show: [big, small]
- at: "big model passes"
highlight: big
- at: "smaller model passes"
highlight: small
- at: "8 out of 10"
show: frac
- at: "one wrong refund"
show: risk
- at: "62 dollars a month"
show: saved Common mistake: a date at the top
- A date at the top
- Changing text at the end
- Check your bill
layout: side
objects:
bd: {text: "Bad: today's date first", pos: [0, 2.4]}
bm: {text: "Cache misses: full price", pos: [0, 1.5]}
gd: {text: "Good: fixed rules first", pos: [0, 0.2]}
gr: {text: "Rules, same every run", pos: [0, -0.6]}
gt: {text: "Date and order at the end", pos: [0, -1.4]}
gc: {text: "Cache hits: a tenth of the price", pos: [0, -2.4]}
steps:
- at: "wastes the most money"
show: bd
- at: "today's date"
highlight: bd
- at: "beginning changes"
show: bm
- at: "fix is simple"
show: gd
- at: "fixed rules at the top"
show: gr
- at: "at the end"
show: gt
- at: "confirm the cache"
show: gc Recap: three levers, one bill
- Trim the prompt, keep the rules
- Pick the smaller model that passes
- Cache the fixed beginning
layout: side
objects:
r0: {text: "The whole lesson", pos: [0, 2.7]}
r1: {text: "Baseline: about 85 dollars", pos: [0, 1.9]}
r2: {text: "Shorter prompt alone: about 53", pos: [0, 1.1]}
r3: {text: "Smaller model alone: about 23", pos: [0, 0.3]}
r4: {text: "Caching alone: about 36", pos: [0, -0.5]}
r5: {text: "All three: about 9", pos: [0, -1.4]}
r6: {text: "Test on tricky messages", pos: [0, -2.4]}
steps:
- at: "one picture"
show: r0
- at: "85 dollar bill"
show: r1
- at: "Trim the system prompt"
show: r2
- at: "Switch to a smaller model"
show: r3
- at: "Cache the fixed beginning"
show: r4
- at: "all three together"
show: r5
- at: "land near 9"
highlight: r5
- at: "tricky messages"
show: r6 Exercise: cost your own agent
- Estimate the cost per run
- Trim, then recount the tokens
- Keep it if the score holds
layout: side
objects:
e0: {text: "About 30 minutes", pos: [0, 2.8]}
e1: {text: "1. Estimate the cost per run", pos: [0, 1.8]}
e2: {text: "2. Multiply by runs a month", pos: [0, 0.9]}
e3: {text: "3. Trim, then recount", pos: [0, 0.0]}
e4: {text: "4. Test on ten tricky messages", pos: [0, -0.9]}
e5: {text: "Keep it only if the score holds", pos: [0, -1.8]}
own: {text: "Use one of your own agents", pos: [3.2, 1.8]}
steps:
- at: "thirty minutes"
show: e0
- at: "Pick one agent"
show: own
- at: "Estimate its cost"
show: e1
- at: "multiply by the number"
show: e2
- at: "trim the system prompt"
show: e3
- at: "write down how many"
show: e4
- at: "score holds"
show: e5 النص المكتوب
A bill for a month of messages. Imagine you run a small bakery that sells cakes online. Your support agent answers customer messages, books pickup times, qualifies leads for custom cakes, sends invoices and reports sales every week. Then the first monthly bill arrives. The agent handled 9,000 messages, and the bill came to about 85 dollars. That is not a disaster, but the bill grows with every single message. In this lesson you will learn how to make each run cheaper without making the agent worse.
What you will learn. By the end of this lesson you will be able to do three things. First, you will trim the system prompt without losing the rules that protect your business. Second, you will pick a smaller model, but only when it still passes your own tests. Third, you will use prompt caching, so the same instructions are not charged again on every single run. These are the three levers you control, and each one sits in a different layer of your agent.
Where the money goes. What are you actually paying for? A token is a small piece of text, roughly three quarters of an English word, and the provider charges for every token that goes in, the input, and every token that comes back out, the output. Think of a bakery that pays for every ingredient, including the recipe book it reads from before each cake. Here is one normal run. The system prompt is 2,000 tokens, the customer message is 150, and the reply is 200. The input is 2,150 tokens at 3 dollars per million, and the output is 200 tokens at 15 dollars per million. Add the two together and the cost of one run is under one cent.
Lever one: a shorter prompt. So how do you shorten a prompt safely? Start by finding text the agent never needs. Many prompts contain repeated rules, old price lists, and long examples written for a different business. Delete those, but keep the safety rules that stop costly mistakes, such as never promising a booking outside opening hours. In the bakery example, the system prompt drops from 2,000 tokens to 800 tokens. The run now sends 800 plus 150, which is 950 input tokens, and the cost falls to about 5.85 dollars per thousand runs.
Lever two: a smaller model. A cheaper model is not automatically the right model. Suppose a smaller model costs 0.80 dollars per million input tokens and 4 dollars per million output tokens. Keep the bakery's long prompt for now, and run the same 2,150 input tokens and 200 output tokens. The input part costs 1.72 thousandths of a dollar, the output part costs 0.8 thousandths, and the total is 2.52 thousandths per run. That is about 2.52 dollars per thousand runs, roughly 73 percent less than the big model, but only if it still handles a tricky message about changing an appointment correctly.
Lever three: prompt caching. Here is the surprising shortcut. Most agents send the same system prompt on every run, and the provider can keep a copy of that fixed beginning ready. When the next request starts with exactly the same text, the provider reads the stored copy, and a cached read costs about a tenth of the normal input price. Only the new part of the message is charged in full. Think of a bakery that keeps its standard cake recipe prepped on the counter instead of rebuilding it each morning. The cache matches from the very start of the prompt, so the changing part, such as today's customer message, must go last.
The caching arithmetic. Slow down here, because this is where the numbers change. Take the original 2,000 token system prompt with caching turned on. The 2,000 cached tokens cost 0.30 dollars per million, which is 0.0006 dollars. Next, the 150 new tokens still cost the full 3 dollars per million, which is 0.00045 dollars. The output is unchanged at 0.003 dollars. Add the three parts and the run costs 0.00405 dollars, or about 4.05 dollars per thousand runs. The first request that writes the cache costs a little extra, and the cache expires after a short idle period.
Stacking the three levers. Let's compare the levers. Each one alone cuts the monthly bill: the shorter prompt brings it to about 53 dollars, the smaller model to about 23, and caching to about 36. Now stack all three. Use the 800 token prompt, the smaller model, and cache those 800 tokens. With the smaller model, a cached token costs a tenth of 0.80, which is 0.08 dollars per million. The run totals about 0.000984 dollars, so the bill for 9,000 messages falls from about 85 dollars to about 9 dollars. Doing all three gives the biggest drop.
Test before you cut. Before you ship a cheaper version, ask one question: does it still handle the hard cases? Take ten tricky messages, such as a double booking, a refund for a cake that arrived late, or a lead who asks for a quote and a discount together. Run them through both versions and count the answers you would be happy to send. Suppose the big model passes 10 out of 10 and the smaller model passes 8 out of 10. Those two misses matter, because one wrong refund answer can cost more than the 62 dollars a month that the smaller model saves.
Common mistake: a date at the top. Here is the mistake that wastes the most money. Many builders add today's date, the customer's name, or the order number at the very top of the system prompt, because it feels natural. Now the beginning changes on every run, so the cache never matches and every token is billed at full price. The fix is simple. Keep the fixed rules at the top, and put the changing text at the end: the date, the name and the order details, which change without breaking the stored copy. Check your bill after one day to confirm the cache is really working.
Recap: three levers, one bill. Let's rebuild the whole lesson in one picture. Start with the 85 dollar bill for 9,000 messages. Trim the system prompt from 2,000 tokens to 800, and on its own it brings the bill to about 53 dollars. Switch to a smaller model, and alone it brings it to about 23. Cache the fixed beginning, and alone it brings it to about 36. Use all three together, and you land near 9 dollars. Then test every cheaper version on your tricky messages before you trust it.
Exercise: cost your own agent. Your exercise before the next lesson takes about thirty minutes. Pick one agent you have built or plan to build, such as the one that answers customer messages. Estimate its cost per run using the input tokens, the output tokens and your provider's prices. Then multiply by the number of runs a month. Next, trim the system prompt, remove repeated rules, and recount the tokens. Finally, write down how many of your ten tricky messages still pass, and keep the cheaper version only if the score holds.
استكشف الرسوم المتحركة
الرسوم المتحركة نفسها التي في الفيديو، دقيقة وتفاعلية: شغّلها أو أوقفها، أو اسحب المؤشر، أو انتقل إلى أي خطوة بالسرعة التي تناسبك.
Loading the animation...
Loading the animation...
Loading the animation...
Loading the animation...
Loading the animation...
Loading the animation...
Loading the animation...
Loading the animation...
Loading the animation...
Loading the animation...
Loading the animation...
Loading the animation...
أجب عن كل الأسئلة ثم تحقّق. تنجح بنسبة 70%، ويمكنك إعادة المحاولة كما تشاء.
ملخص الدرس
- القراءة
- مشاهدة الفيديو
- اجتياز الاختبار (70%)
- Trim the prompt, keep the rules
- Pick the smaller model that passes
- Cache the fixed beginning