Track cost and operational health
Set budgets, record useful logs, and notice when a workflow degrades. This module has 3 lessons and takes about 1h 50m. Each lesson has a practice exercise that runs real Python in your browser and a short quiz.
Start module 17 in the Learning LabEstimate a run cost
Given non-negative counts and rates per million tokens, return estimated cost rounded to six decimals.
AI services may charge separately for input and output tokens. Prices and token accounting differ by provider, so use current provider values for a real estimate.
This exercise receives fictional prices per million tokens as parameters. Multiply each token count by its rate, add the results, and divide by one million. Round the result to six decimals.
This is a cost calculation, not a current price quote. Real bills may include caching, tools, storage, and other charges.
See it in action
def solve(input_tokens, output_tokens, input_rate, output_rate):
return round((input_tokens * input_rate + output_tokens * output_rate) / 1_000_000, 6)
print(solve(1000, 200, 2, 8))
This is a cost calculation, not a current price quote. Real bills may include caching, tools, storage, and other charges.
Keep useful, minimal logs
Return only present run_id, stage, status, and version fields from the dictionary record.
A run log connects the event, stage, outcome, and workflow version. It helps find failures without saving every prompt or private message.
Use an allowlist to copy only run_id, stage, status, and version from the input record. Missing fields are omitted. Unlisted values such as secrets and raw text are dropped.
Data minimisation reduces exposure but is not complete privacy protection. Control log access, retention, and the content allowed inside each field.
See it in action
def solve(record):
return {key:record[key] for key in ("run_id","stage","status","version") if key in record}
print(solve({"run_id":"r1","stage":"classify","status":"review","version":"v1","api_key":"secret","message":"private"}))
Data minimisation reduces exposure but is not complete privacy protection. Control log access, retention, and the content allowed inside each field.
Practical: Stop when a budget or error limit is reached
For valid non-negative counts and costs, pause at the budget cap or an error rate of at least 20 percent. Otherwise continue.
Operational limits should be defined before the pilot. A budget cap and an error threshold can pause a workflow for investigation.
The example pauses when spent is at least budget or when failures divided by runs is at least 0.2. With zero runs, only the budget condition applies.
Choose thresholds using the expected workload and error cost. A tiny sample can fluctuate, so show counts as well as rates to the owner.
Your experiment plan
Use the browser editor or the notebook. All records and AI responses are fictional. No network calls, credentials, or real messages are used.
- Define a fictional weekly budget, a run log schema, and an escalation owner.
- Run the example and predict its result before changing it.
- Complete the function in Practice and run the checks. Add your own normal, empty, and failure cases.
- Test the exact budget and error-rate boundaries.
- Save the notebook with your observations and explain one limitation of the simulation.
What to hand in
- A cost estimate, a minimal log, and a pause policy.
- A test table with inputs, expected outputs, actual outputs, and one revision.
Reflect on your results
What information does the owner need to decide whether to resume?
The automatic exercise checks the core code behaviour. Review observations and written deliverables yourself or with a mentor.
See it in action
def solve(spent, budget, failures, runs):
if spent >= budget:
return "pause"
if runs > 0 and failures / runs >= 0.2:
return "pause"
return "continue"
print(solve(5, 10, 1, 10))
Choose thresholds using the expected workload and error cost. A tiny sample can fluctuate, so show counts as well as rates to the owner.
Free, open and run by a nonprofit.
This page is part of AI Automation, a free course of 20 modules and 60 lessons in the engineering.fit( ) Learning Lab. The Learning Lab is built and run by engineering.fit( ), a nonprofit founded by Abdullateef Opeyemi Bakare that helps African engineering students gain research experience. You do not need an account or pay a fee to learn.