CSE Roadmap

Core tier · about 6 hours · 8 parts

Part 7 of 8 · 3 min read

Exercises

Try each exercise before opening its hint. Use your provider's current prices when asked.

Easy

Easy 1: One call by hand (15 minutes)

A call has 4,000 input tokens and 500 output tokens. Using the example prices, compute its cost on paper. Then run it through agent_run_cost with steps=1, system_tokens=4000 and the other token counts 0 (use 500 for the output).

Done when: your hand result and the code agree to the cent.

Hint

4,000 x 3 / 1,000,000 = $0.012 for input.

Easy 2: Predict the doubling (20 minutes)

Using the table, predict (before running) the input tokens for 80 steps with the demo parameters, then compute it with the code and with the closed form.

Done when: all three numbers agree and you can say by what factor 80 steps exceeds 40.

Hint

the quadratic term is R x n(n - 1) / 2 with R = 950.

Easy 3: Read the latency numbers (15 minutes)

For a 2,000-token input and 100-token reply, use latency_seconds and say which part dominates. Then repeat with a 40,000-token input and a 100-token reply.

Done when: you can explain which part changed and why.

Medium

Medium 1: Price your own agent (60 minutes)

Choose an agent you might build. Estimate system prompt, tool result and output sizes. Using current prices from a provider, compute the cost per task, per 1,000 tasks, and per month at a volume you choose. Show it with and without caching.

Done when: you have a table of three scenarios and a sentence naming the largest cost driver.

Medium 2: Find the break-even for trimming (60 minutes)

Using the code, find the step count at which trimming to the last 3 rounds becomes cheaper than not trimming. Then repeat with keep_last = 1, 3, 6. Then repeat the three searches with caching turned on and explain the difference.

Done when: you have six break-even numbers and a note on what you lose by trimming.

Hint

compute the difference in a loop over step counts and find where it changes sign.

Medium 3: Retries that behave (60 minutes)

Write call_with_retry(fn, attempts, ...) using backoff_delays. It retries only on exceptions you name, never on others, and takes an injectable sleep function. Test with a function that fails twice then succeeds, one that always fails, and one that raises a non-retryable error.

Done when: all three tests pass without any real sleeping.

Hint

pass sleep=lambda s: None in tests, and record the delays it was called with.

Hard

Hard 1: Simulate a thundering herd (90 minutes)

Simulate 500 clients that all fail at time 0 against a server that can accept 20 requests per second. Compare three retry policies: fixed 1 s delay, exponential without jitter, and exponential with full jitter. Report how many seconds until everyone succeeds and the peak requests in any second.

Done when: jitter shows a clearly lower peak, and you have the three result rows.

Hint

model time in whole seconds; each second the server accepts the first 20 waiting requests.

Hard 2: A budget guard (120 minutes)

Wrap an agent loop (a fake model and fake tools are fine) with a guard that stops the run when it exceeds a token budget, a step limit or a dollar limit, and returns the reason. Test each limit separately and test that a run under all limits completes.

Done when: four tests pass and the stop reason is reported in each case.

Hard 3: Rate limiter under concurrency (120 minutes)

Using asyncio, run 200 tasks that each need 1 token from a shared token bucket (rate 20 per second, capacity 20). Use a fake clock or asyncio.sleep and verify that the number of requests started in any one-second window never exceeds 40 (burst plus refill), and that all 200 finish.

Done when: both assertions are in a test and pass reliably.

Hint

the bucket's wait_time tells you how long to await asyncio.sleep.