الدرس 5 · الوحدة 1: System Prompts That Control Agents and Cut Cost
Tokens and pricing: measuring what each agent run really costs
- Count tokens
- Price each call
- Measure real runs
Why this matters
Your booking From the lesson: How an AI agent works: model, instructions, tools and memoryA language model given instructions, tools and memory so it can complete a task.Review that lesson can answer a customer in seconds, but every reply is billed in small pieces, and the bill depends on how the agent is built. Builders who estimate cost from the reply they can see often miss most of the real charge. Counting every piece of a run gives you the true price per customer and a reliable monthly forecast.
Key ideas
- Tokens are the unit of billing. A token is a small piece of text, usually about four letters, so 100 tokens comes to roughly 75 words. Numbers, code and some languages split into more tokens than plain English does.
- Input and output are priced apart. Input is everything you send to the model, and output is what it writes back. Prices are quoted per million tokens, and output is usually the dearer side. Worked example: 2,000 input tokens at 3 dollars per million cost 2,000 × 3 ÷ 1,000,000 = 0.006 dollars.
- Every call resends the context. The model keeps no memory between calls, so each one carries the whole everything the model reads on one call, sent again each time: the From the lesson: Anatomy of a system prompt: role, goal, rules, output formatThe standing instructions that tell an agent its role, goal, rules and output format.Review that lesson, tool descriptions, the customer's message and every earlier step. Input grows with each call.
- A run is the sum of its calls. One customer message can trigger several calls, and a what a tool, such as a calendar check, sends back to the agent becomes part of the next call's input. Each reply carries a usage record, so add the counts across the run before multiplying by the prices.
Try it before you watch
An agent sends 1,000 input tokens and gets back 100 output tokens. Input costs 3 dollars per million tokens and output costs 15 dollars per million. What does this one call cost?
Show answerHide answer
Input: 1,000 × 3 ÷ 1,000,000 = 0.003 dollars. Output: 100 × 15 ÷ 1,000,000 = 0.0015 dollars. Total: 0.0045 dollars. Each side is priced on its own, then the two are added.
Words to know
- token: a small piece of text, such as a word part, a short word or a punctuation mark, that a model counts and bills.
- input: everything sent to the model in one call, including instructions and earlier messages.
- output: the text the model writes back in one call.
- price per million tokens: the rate charged for one million tokens, quoted separately for input and output.
- usage record: the counts of input and output tokens that come back with each model reply.
- run: one customer message handled from start to finish, including every model call it causes.
What to watch for
- The three calls in the calendar example, and how each one re-reads what came before.
- The two lines of arithmetic: which side makes most of the bill, and why.
- The cost chart, where the reply-only figure falls far short of the whole run.
تعلّمه مع معلمك على السبورة المباشرة
أهم نقاط الفيديو
A message at nine at night
- Nine at night
- Agent answers in seconds
- Billed, one small piece at a time
layout: full
parts:
llm: {chip: LLM, sub: "Language model"}
agent: {box: AGENT, holds: [llm], icon: bot}
ask: {chat: "Can I book a haircut Thursday?", reply: "Yes, Thursday at 4 pm is free."}
steps:
- at: "messages your hair salon"
send: ask
- at: "Your booking agent"
highlight: llm
- at: "your agent handles"
highlight: agent
- at: "serve one customer"
note: "One run, one bill" What you will learn
- Count tokens
- Price each call
- Measure real runs
layout: full
parts:
run: {chip: Agent run, sub: "One customer message"}
steps:
- at: "Here is what you will do"
spokes: {of: run, items: [Count tokens, Price each call, Measure every run]}
- at: "small pieces that a language model"
note: "Pieces of text"
- at: "are billed differently"
note: "Input and output priced apart"
- at: "so you stop guessing"
note: "No more guessing"
- at: "your own provider's"
note: "Swap in your own prices" What a token is
- Small chunks of text
- About four letters
- Split into several tokens
layout: full
objects:
question: {tex: '\text{What is a token?}', pos: top}
reads: {tex: '\text{letters or whole words}', pos: top}
chips: {tex: '\text{Can} \mid \text{ I} \mid \text{ book} \mid \text{ a} \mid \text{ hair} \mid \text{ cut}', pos: center}
rule: {tex: '1 \text{ token} \approx 4 \text{ letters}', pos: top}
ratio: {tex: '4 \text{ letters} \approx \frac{3}{4} \text{ word}', pos: center}
hundred: {tex: '100 \text{ tokens} \approx 75 \text{ words}', pos: bottom}
split: {tex: '\text{unusual} \rightarrow \text{un} \mid \text{usu} \mid \text{al}', pos: bottom}
numbers: {tex: '2104 \rightarrow 21 \mid 04', pos: bottom}
arabic: {tex: '\text{Arabic: a different count}', pos: bottom}
steps:
- at: "So what is a token"
show: question
- at: "does not read letters"
morph: [question, reads]
- at: "called tokens"
show: chips
- at: "A token is usually"
highlight: chips
- at: "one token is about"
morph: [reads, rule]
- at: "three quarters of a word"
morph: [chips, ratio]
- at: "a hundred tokens"
show: hundred
- at: "split into several tokens"
morph: [hundred, split]
- at: "Numbers and code"
morph: [split, numbers]
- at: "depends on the language"
morph: [numbers, arabic] Input and output, two prices
- Input is everything you send
- Output: the model writes back
- Usually the more expensive
kind: bar
title: Example price per million tokens
unit: " USD"
data: {Input: 3, Output: 15}
steps:
- at: "two sides"
show: [Input]
note: "Input: what you send"
- at: "Output is what"
show: [Input, Output]
note: "Output: what it writes"
- at: "quoted per million tokens"
note: "Dollars per million tokens"
- at: "costs five times as much"
highlight: Output
- at: "your own provider's price page"
note: "Check the live price page" One run, several model calls
- Check the calendar
- Three model calls
- A separate bill
layout: side
direction: LR
components:
cust: {kind: user, label: Customer}
agent: {kind: service, label: Booking agent}
llm: {kind: llm, label: Language model}
cal: {kind: external, label: Calendar}
links: [cust->agent, agent->llm, agent->cal]
steps:
- at: "sends a message"
show: [cust, agent, llm]
flow: ["cust>agent: message", "agent>llm: call 1"]
- at: "asks to check"
show: [cal]
flow: ["llm>agent: asks"]
- at: "runs that check"
flow: ["agent>cal: check", "cal>agent", "agent>llm: call 2"]
- at: "reads the free times and picks one"
flow: ["llm>agent: picks 4 pm"]
- at: "calls once more to write the reply"
flow: ["agent>llm: call 3", "llm>agent: reply", "agent>cust: reply"]
- at: "So one customer message"
note: "Three calls, three bills"
- at: "Each of those calls"
note: "Each call is billed"
- at: "add up fast"
note: "Calls add up fast" Every call sends the context again
- Call 1: 2,050 tokens
- Call 2: 2,310 tokens
- Call 3: 2,540 tokens
- Total: 6,900 input tokens
kind: bar
title: Input tokens per call
data: {First call: 2050, Second call: 2310, Third call: 2540}
steps:
- at: "no memory between calls"
show: [First call]
note: "Every call starts from scratch"
- at: "sends everything again"
note: "Everything, every time"
- at: "The first call reads"
highlight: First call
- at: "The second call reads"
show: [First call, Second call]
highlight: Second call
- at: "The third call reads"
show: all
highlight: Third call
- at: "Altogether, that run sent"
note: "Total: 6,900 input tokens" Measure every run in code
# Usage per call, as the API reports it.
# Numbers are from one booking run (example).
PRICE_IN = 3.00 # dollars per million input tokens
PRICE_OUT = 15.00 # dollars per million output tokens
calls = [
{"step": "read", "in": 2050, "out": 60},
{"step": "check", "in": 2310, "out": 80},
{"step": "reply", "in": 2540, "out": 120},
]
tot_in = sum(c["in"] for c in calls)
tot_out = sum(c["out"] for c in calls)
cost = (tot_in * PRICE_IN + tot_out * PRICE_OUT) / 1_000_000
print("input tokens:", tot_in)
print("output tokens:", tot_out)
print(f"one message: {cost:.4f} dollars")
print(f"1000 messages: {cost * 1000:.2f} dollars") The arithmetic, line by line
- Input: 0.0207 dollars
- Output: 0.0039 dollars
- One message: 0.0246 dollars
- A thousand runs: 24.60 dollars
layout: side
objects:
eqIn: {tex: '\frac{6900 \times 3}{1000000} = 0.0207', pos: top}
eqOut: {tex: '\frac{260 \times 15}{1000000} = 0.0039', pos: center}
eqTot: {tex: '0.0207 + 0.0039 = 0.0246', pos: bottom}
eqMonth: {tex: '0.0246 \times 1000 = 24.60', pos: bottom}
steps:
- at: "Input first"
show: eqIn
- at: "Then output"
show: eqOut
- at: "Add the two"
show: eqTot
- at: "Scale that up"
morph: [eqTot, eqMonth]
- at: "most of that bill"
highlight: eqMonth Common mistake: counting only the reply
- Only at the reply
- Repeated input
- Missing tool results
kind: bar
title: Cost per message, dollars
data: {Reply only: 0.0018, Whole run: 0.0246}
steps:
- at: "Many builders look"
show: [Reply only]
- at: "shows a cost of"
highlight: Reply only
- at: "the real run costs"
show: all
- at: "The missing part is"
note: "Missing: 0.0228 dollars"
- at: "Measure the whole run"
note: "Every call, every time" Exercise: price one real run
# Your task: one run of your own agent.
# Replace the numbers with your real counts.
PRICE_IN = 3.00 # from your provider's price page
PRICE_OUT = 15.00
calls = [
{"in": 2000, "out": 50},
{"in": 2300, "out": 90},
]
tot_in = sum(c["in"] for c in calls)
tot_out = sum(c["out"] for c in calls)
cost = (tot_in * PRICE_IN + tot_out * PRICE_OUT) / 1_000_000
print("input tokens:", tot_in)
print("output tokens:", tot_out)
print(f"one message: {cost:.4f} dollars")
print(f"1000 messages: {cost * 1000:.2f} dollars") Recap
- Count every token
- Multiply each side
- Add up every call
layout: full
parts:
run: {chip: One run, sub: "Three steps"}
steps:
- at: "Let's put the lesson together"
spokes: {of: run, items: [Count every token, Multiply each side, Add up every call]}
- at: "on both the input side"
note: "Input and output tokens"
- at: "output is usually the pricier one"
note: "Output costs more"
- at: "not just the final reply"
note: "Every call counts"
- at: "Keep those three steps"
note: "Save the cost with each message" النص المكتوب
A message at nine at night. Picture this. It is nine at night, and a customer messages your hair salon to ask for a haircut on Thursday afternoon. Your booking agent answers in seconds, while you are asleep. That reply felt free, but it was not. Every message your agent handles is billed, one small piece at a time, and the bill depends on how the agent works. By the end of this lesson you will know how to count those pieces, put a price on one whole run, and say what it really costs to serve one customer.
What you will learn. Here is what you will do in this lesson. First, you will count tokens, the small pieces that a language model reads and writes. Second, you will price each call your agent makes, because input and output are billed differently. Third, you will measure real runs with a few lines of code, so you stop guessing. You will use one example all the way through: a salon booking agent, with a small price list you can swap for your own provider's.
What a token is. So what is a token? A language model does not read letters or whole words the way you do. It reads small chunks of text called tokens. A token is usually a short word, a part of a longer word, or a punctuation mark. As a rough rule for English, one token is about four letters, which means three quarters of a word on average. So a hundred tokens come to about seventy-five words. Long or unusual words split into several tokens. Numbers and code often split too. The count depends on the language, so the same meaning in Arabic can use a different number of tokens than it does in English.
Input and output, two prices. Here is where the money goes. Every model call has two sides, and each side has its own price. Input is everything you send to the model: the instructions, the tool descriptions, the earlier messages and the new question. Output is what the model writes back. Prices are quoted per million tokens, and output is usually the more expensive side. In the example on screen, input costs 3 dollars per million tokens and output costs 15, so one output token costs five times as much as one input token. Check your own provider's price page, because prices change.
One run, several model calls. Now let's follow one customer through the system. The customer sends a message, and the booking agent passes it to the model as call one. The model does not answer yet. It asks to check the calendar. The agent runs that check and sends the free times back to the model as call two. The model reads the free times and picks one. Then the agent calls once more to write the reply: call three. So one customer message produced three model calls, and a changed mind means more. Each of those calls is a separate bill. Multiply that by dozens of customers a day, and the calls add up fast.
Every call sends the context again. Here is the trap most people miss. The model has no memory between calls. So on every call, the agent sends everything again: the instructions, the tool descriptions, the customer's message, and every step so far. Look at the numbers on screen. The first call reads 2,050 input tokens. The second call reads 2,310, because the first call's output and the calendar result were added. The third call reads 2,540. Altogether, that run sent 6,900 input tokens, even though the customer only typed one short question.
Measure every run in code. Guessing is not measuring. Every model reply carries a usage record: a small block of numbers that says how many input and output tokens the model counted. Your code adds those up for each run, multiplies by the two prices, and saves the result next to the customer's message. The code on screen does that for the three calls from the last slide. Paste it into a free online Python runner, or into a Code step of your no-code tool if it has one. Many no-code tools also show each step's token counts in their run history, so you can type those into the calls list. Run it, and it prints the total tokens, the cost of one message, and the cost of a thousand messages.
The arithmetic, line by line. Slow down for the arithmetic, because it is simple but easy to rush. Input first: 6,900 tokens times 3 dollars, divided by a million, gives 0.0207 dollars. Then output: 260 tokens times 15 dollars, divided by a million, gives 0.0039 dollars. Add the two, and one message costs 0.0246 dollars. Scale that up for a month, and a thousand runs cost 24.60 dollars. Input is most of that bill, because the whole context is paid again on every call.
Common mistake: counting only the reply. Here is the mistake that makes budgets fail. Many builders look only at the reply, the part they can read on screen, and multiply its length in tokens by the output price. That shows a cost of 0.0018 dollars per message. But the real run costs 0.0246 dollars, and the reply is less than a tenth of it. The missing part is the repeated input, the tool results and the earlier calls. In a small business, that gap decides whether the agent pays for itself. Measure the whole run, every call, every time, or your forecast will be wrong by a wide margin.
Exercise: price one real run. Now it is your turn, and it takes about ten minutes. Paste the code on screen into a free online Python runner, or into a Code step of your no-code tool. Replace the example numbers with the input and output counts from one real or imagined run of your own agent. If your tool shows those counts in its run history, copy them from there. No code at all? Put the input counts in one spreadsheet column and the output counts in another, then type a formula that multiplies each total by its price and divides by a million. Next, change the prices to the ones on your provider's price page. Then run it, and write down the cost of one message and the cost of a thousand. Keep that number, because next lesson you will learn how to bring it down.
Recap. Let's put the lesson together. First, count every token in a run, on both the input side and the output side. Second, multiply each side by its own price, because output is usually the pricier one. Third, add up every call the agent makes, not just the final reply. Keep those three steps in your code, save the cost next to each customer message, and you will know what a customer really costs you.
استكشف الرسوم المتحركة
الرسوم المتحركة نفسها التي في الفيديو، دقيقة وتفاعلية: شغّلها أو أوقفها، أو اسحب المؤشر، أو انتقل إلى أي خطوة بالسرعة التي تناسبك.
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...
تعمل لغة Python داخل متصفحك دون أي تثبيت. في أول تشغيل يُنزَّل Python (نحو 20 ميجابايت، ثم يُحفظ في المتصفح).
1 Price one run
Write a function run_cost(calls) that takes a list of calls. Each call is a dictionary with the keys in (input tokens) and out (output tokens). Return the dollar cost of the whole run, using the prices in the starter. Add all the input tokens and all the output tokens first, multiply each total by its price, add the two, and divide by 1,000,000.
أجب عن كل الأسئلة ثم تحقّق. تنجح بنسبة 70%، ويمكنك إعادة المحاولة كما تشاء.
ملخص الدرس
- القراءة
- مشاهدة الفيديو
- التمارين
- اجتياز الاختبار (70%)
- Count every token
- Multiply each side
- Add up every call