VKVlad KulikovPublic / lectures
Modern LLMs18 / 20

Module 18. Agents and Tool Use

Try each exercise first. Expand worked answers or project guidance when needed; their open state is shared between RU/EN.

Download course files (.zip) 2588.1 KB

The archive preserves the source folder structure, both languages, and supporting files.

E1. Context and step latency

A loop begins with 3,000 tokens and appends 1,500 on each of 20 steps. Compare full-history prefill with prefix reuse and find final KV at 128 KiB/token. For one step, add prefill of 1,500 tokens for an 8.03B model at 495 TFLOP/s, decode of 90 tokens at 209.4 tok/s, and a 300 ms tool call.

Show answer and solution

Solution. Without reuse, step ii processes full history 3000+1500i3000+1500i. The arithmetic progression gives 20⋅3000+1500(0+⋯+19)=34500020\cdot3000+1500(0+\cdots+19)=345000 tokens. Reuse processes initial prefix once and the 19 increments preceding calls once: 3000+19⋅1500=315003000+19\cdot1500=31500, a 10.9524 ratio of processed input positions, excluding the growing cost of reading old KV. Final KV is 33000⋅12833000\cdot128 KiB = 4.0283 GiB.

The conditional 2PT2PT prefill estimate is 2⋅8.03B⋅1500/(495 TFLOP/s)=48.6672\cdot8.03\text{B}\cdot1500/(495\text{ TFLOP/s})=48.667 ms. Decode is 90/209.4=429.79990/209.4=429.799 ms and tool latency 300 ms. Total is 778.466 ms, with decode at 55.21%. This is a conditional scenario without queueing or runtime overhead.

E2. Reliability and parallel accounting

Compute twenty-step success for step probabilities 0.98 and 0.95, without and with one independent retry. Find the per-step probability required for 90% success over 50 steps. Then analyze 20 subagents with 400 work minutes, 5 planning minutes, 20 merge minutes, 30K tokens per worker, and 128 KiB KV/token.

Show answer and solution

Solution. Without retry, every step must succeed: 0.9820=0.667610.98^{20}=0.66761 and 0.9520=0.358490.95^{20}=0.35849. With one independent retry, per-step success is 1−(1−p)21-(1-p)^2, or 0.9996 and 0.9975; raising these to the twentieth power gives 0.99203 and 0.95117. Requiring p50=0.9p^{50}=0.9 gives p=0.91/50=0.997895p=0.9^{1/50}=0.997895.

Perfect partitioning makes worker time 400/20=20400/20=20 minutes and critical path 5+20+20=455+20+20=45 minutes. Speedups are 400/45=8.89×400/45=8.89\times against useful work and (5+400+20)/45=9.44×(5+400+20)/45=9.44\times end-to-end. Even infinitely many workers leave 25 serial minutes, imposing 16× and 17× ceilings. KV is 20⋅30000⋅12820\cdot30000\cdot128 KiB = 73.242 GiB. Retry independence and perfect partitioning are strong assumptions.

E3. CodeAct and a reproducible trace

Five JSON actions use 90 tokens each; one CodeAct block uses 160 tokens. Compute both ratios and interpret 150000/(2⋅8.03B)150000/(2\cdot8.03\text{B}). Then reproduce a trace that repairs a failed call and returns 27.92.

Show answer and solution

Solution. The JSON path uses 5⋅90=4505\cdot90=450 tokens and five model/tool round trips; CodeAct uses 160 tokens and one. The ratios are 450/160=2.8125×450/160=\mathbf{2.8125\times} and 5/1=5×5/1=\mathbf{5\times}. The value 150000/(2⋅8.03B)=9.33998⋅10−6150000/(2\cdot8.03\text{B})=9.33998\cdot10^{-6} compares 150K simple mask checks with rough 2P2P arithmetic for one model token; the work units differ, so the ratio is not a measured time fraction.

The reproducible trace first calls lookup(sensor_bach) and receives tool_error; it corrects the key to sensor_batch and obtains 13.96; then it calls the calculator with 13.96 * 2, obtains 27.92, and returns Answer: 27.92. Event kinds are assistant_action → tool_error → assistant_action → tool_result → assistant_action → tool_result → assistant_final → verification_pass, with three registry invocations, one error, and status completed. The invariant is that the failure becomes an observation rather than uncontrolled execution.

E4 [project]

Build a ReAct-style agent with search, calculator, and a safe local executor. On 20 tasks, report task success, protocol-error rate, tool-error rate, steps, and cost. Then attach a read-only MCP server and repeat the evaluation.

Project demos and guidance

Demo for this project exercise: English project-demo notebook for Module 18, section “Demo 1”. It shows a compact reproducible example on small inputs, not a completed solution to the full-scale project.

E5 [project]

Divide analysis of a ten-file project among subagents. Compare sequential and parallel versions by wall-clock time, total tokens, KV memory, duplicated work, and merge conflicts. Add checkpoint recovery after one worker failure.

Project demos and guidance

Demo for this project exercise: English project-demo notebook for Module 18, section “Demo 2”. It shows a compact reproducible example on small inputs, not a completed solution to the full-scale project.