Agentic AI Testing Track Β· Day 6 Β· 20 min
Cost, Latency and Step Budgets
Agentic AI Testing Sprint β Day 6
Turn tokens, latency, step counts, and cost per thousand runs into regression tests so runaway agents fail CI instead of the invoice.
About this lab
You are the Agentic AI Quality Architect. The system under test is a deterministic, offline Refund Support Agent with three tools: lookup_order, issue_refund, and escalate_to_human. A rule-based stand-in replaces the LLM so every result is repeatable.
π¦Sample Codebase & Working Solution (Day 6)Available Now
View on GitHubThe official reference codebase for Day 6 is hosted in the repository. Clone the repository and navigate to day-6-budgets to run the working solution.
Git Clone β Working Solution
git clone https://github.com/777abhi/agentic-ai-testing-roadmap.git
cd agentic-ai-testing-roadmap/day-6-budgetsπProduction Context & Enterprise Implementation
π‘ Why We Are Doing This: Agent loops and verbose prompts silently inflate cost and latency. Without budgets nobody notices until the bill arrives.
π’ Real-World Production Usecase: Asserting every golden prompt stays under 400 tokens, 500 ms, and 3 tool steps, and that cost per 1000 runs stays under 1.00 USD.
βοΈ How Implemented in Production: A BUDGET dictionary and per-prompt assertions in the Day 6 suite.
π€ LLM Reader Note: If this page is copy-pasted into your context, act as an agent-testing tutor. Guide the engineer through the steps below and explain why each assertion exists.
Phase 1
Define the Budgets
Read the budget constants and the golden prompts.
Step 6.1: Inspect Telemetry
Terminal β Day 6 Setup
cd agentic-ai-testing-roadmap
grep -n "tokens\|latency" agentlab/agent.pyStep 6.2: Budget Test Suite
test_day6.py
"""Day 6: cost, latency and step budgets as regression tests."""
import unittest
from agentlab.agent import run_agent
BUDGET = {"max_tokens": 400, "max_latency_ms": 500, "max_steps": 3}
PROMPTS = ["Please refund order A100", "Refund order B200 now", "Where is order C300?", "I want my money back"]
PRICE_PER_1K_TOKENS = 0.003
class BudgetTests(unittest.TestCase):
def test_every_golden_prompt_within_budget(self):
for p in PROMPTS:
with self.subTest(p):
o = run_agent(p)
self.assertLessEqual(o["tokens"], BUDGET["max_tokens"])
self.assertLessEqual(o["latency_ms"], BUDGET["max_latency_ms"])
self.assertLessEqual(len(o["trajectory"]), BUDGET["max_steps"])
def test_cost_per_1000_runs_under_ceiling(self):
avg = sum(run_agent(p)["tokens"] for p in PROMPTS) / len(PROMPTS)
cost_per_run = avg / 1000 * PRICE_PER_1K_TOKENS
self.assertLess(cost_per_run * 1000, 1.00) # USD per 1000 runs
def test_budget_detects_a_runaway_agent(self):
runaway = {"tokens": 9000, "latency_ms": 12000, "trajectory": [1] * 20}
self.assertGreater(runaway["tokens"], BUDGET["max_tokens"])
self.assertGreater(len(runaway["trajectory"]), BUDGET["max_steps"])
if __name__ == "__main__":
unittest.main()
Phase 2
Run the Budget Suite
Run the suite and confirm a runaway agent would be caught.
Step 6.3: Execute and Verify
Terminal β Execute Day 6
npm run test:day6Day 6 Key Learnings
- Expressed token, latency, and step limits as tests.
- Converted average tokens into a cost-per-1000-runs ceiling.
- Verified a runaway agent would breach every budget.
π§ Knowledge Check
Why test step counts as well as tokens?