Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Is an AI feature worth its cost

After this lesson: I can show whether an AI feature is worth what it costs.

60 min lesson6 commandsAI coding agent lab: use opencode to explain and fix code, and check its work

Cheap per message, dear per problem

A shop adds a chat feature. The team proudly says each message costs almost nothing. The manager asks a different question: how much does it cost to solve one customer problem?

The two numbers can be far apart. Showing that a feature is worth its cost means picking the right one.

By the end of this lesson

Note

I can show whether an AI feature is worth what it costs.

Key words

WordWhat it means
Cost per messagetotal cost divided by every message
Cost per resolved tasktotal cost divided by tasks that were solved
Resolvedthe person got what they needed
Baselinehow the job was done before the feature
Hidden costa cost that is real but left out

The cells students fill in are left blank in this preview.

Two costs that are not the same

Add up everything the feature cost. Divide by the number of messages and you get a small number. Divide by the number of problems solved and you get the number that matters.

Unsolved chats still cost money. Module 69 shows how to count tokens and price a call. Here we use those totals.

Compare with how it was done before

A cost means little alone. Compare it with the baseline: how much did it cost, and how long did it take, before the feature?

Count the hidden costs, such as the person who takes over. A feature that looks cheaper can lose once they are counted.

A common mistake

Warning

Many people think the model price is the whole cost. It is only part. A person who takes over and a chat that is not solved both cost money too. Always divide the whole cost by the tasks that were actually solved.

Write the model helper, part 1

Command
docker exec -w /home/node/project agent sh -c "printf '%s\n' 'const base = process.env.AI_URL || \"http://ai:4000\";' '// The token counts of a reply, or null when the reply carries none.' 'function count(data, name) {' '  return data.usage && typeof data.usage[name] === \"number\" ? data.usage[name] : null;' '}' > model.js"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Write the model helper, part 2

Command
docker exec -w /home/node/project agent sh -c "printf '%s\n' 'async function ask(messages, sample, max) {' '  try {' '    const res = await fetch(base + \"/v1/chat/completions\", {' '      method: \"POST\",' '      headers: { \"content-type\": \"application/json\", authorization: \"Bearer lab\" },' '      body: JSON.stringify({ model: \"lab\", messages: messages, max_tokens: max || 100 }),' '      signal: AbortSignal.timeout(30000),' '    });' '    if (res.ok === false) throw new Error(\"status \" + res.status);' '    const data = await res.json();' '    const text = data.choices[0].message.content;' '    return { text, inTokens: count(data, \"prompt_tokens\"), outTokens: count(data, \"completion_tokens\"), recorded: false };' '  } catch (error) {' '    console.log(\"Recorded sample: no model answered (\" + error.message + \").\");' '    return { text: sample, inTokens: null, outTokens: null, recorded: true };' '  }' '}' 'module.exports = { ask };' >> model.js"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Write the cost checker, part 1

Command
docker exec -w /home/node/project agent sh -c "printf '%s\n' 'const { ask } = require(\"./model\");' 'const PRICE = { inCost: 300, outCost: 1500, human: 2000000 };' 'const RUNS = [' '  { id: \"r1\", tokensIn: 1000, tokensOut: 200, resolved: true, handed: false },' '  { id: \"r2\", tokensIn: 2000, tokensOut: 500, resolved: true, handed: true },' '  { id: \"r3\", tokensIn: 1500, tokensOut: 100, resolved: false, handed: false },' '  { id: \"r4\", tokensIn: 500, tokensOut: 50, resolved: true, handed: false },' '];' > worth.js"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Write the cost checker, part 2

Command
docker exec -w /home/node/project agent sh -c "printf '%s\n' 'function callCost(r) {' '  return r.tokensIn * PRICE.inCost + r.tokensOut * PRICE.outCost;' '}' 'function total() {' '  let sum = 0;' '  for (const r of RUNS) sum += callCost(r) + (r.handed ? PRICE.human : 0);' '  return sum;' '}' >> worth.js"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Write the cost checker, part 3

Command
docker exec -w /home/node/project agent sh -c "printf '%s\n' 'async function main() {' '  const resolved = RUNS.filter(function (r) { return r.resolved; }).length;' '  console.log(\"total \" + total() + \" micro-cents\");' '  console.log(\"per message \" + Math.floor(total() / RUNS.length));' '  console.log(\"per resolved \" + Math.floor(total() / resolved));' '  console.log(\"resolved \" + resolved + \" of \" + RUNS.length);' '  const msg = \"Write one short line saying a cost per message can look low while a cost per solved problem is high.\";' '  const reply = await ask([{ role: \"user\", content: msg }], \"A low cost per message can hide the cost of problems left unsolved.\", 30);' '  console.log(\"verdict: \" + reply.text);' '}' 'main();' >> worth.js"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Run the cost checker

Command
docker exec -w /home/node/project agent node worth.js

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

One possible output

LineWhat it shows
total 4775000 micro-centsFour calls plus one person
per message 1193750Looks low
per resolved 1591666Higher, because r3 was not solved
resolved 3 of 4The number we divide by

The cells students fill in are left blank in this preview.

About this lab and its source: AI coding agent lab: use opencode to explain and fix code, and check its work

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.