Module 6, Lecture 6.4 | Building the Coding Agent
This lecture closes Module 6 by connecting tool calling to the context management work that follows in Module 7. Once an agent can call tools, its real work happens in long, multi-step cycles — and the central insight is that the model does not plan ahead. It generates the next tool call, one at a time, and the "plan" is an emergent chain that exists only because each tool result re-enters the context and shapes the following decision. That emergence is what makes agents flexible enough to improvise, but it is also fragile: the lecture names the three failure modes every agent developer encounters — the infinite loop, the premature stop, and context exhaustion — and shows lightweight mitigations, including iteration caps, clear completion criteria, and prompting the agent to outline its plan first (chain-of-thought from Lecture 5.1, applied inside the loop). The second half turns to instrumentation: counting tokens per API call with a reusable TokenTracker, watching context grow monotonically with every file read, and reasoning about the real cost of an agent session. Seeing the growth problem clearly is the whole point — managing it is the subject of Module 7.
Read the full lecture narrative
usage object, input_tokens/output_tokens, and the count_tokens endpoint for measuring context size before sending a request. Maps directly to the TokenTracker built in this lecture.