Module 5, Lecture 5.3 | Section 3: Prompt and Context Engineering
This lecture puts everything together: system prompt, tool schemas, and working agent code. Starting from the three starter tools (list_files, read_file, edit_file), it builds the complete system prompt section by section — identity, tool descriptions, behavioral guidelines, constraints, and a few-shot example. It then introduces two Python programs: code-agent-v0.py, a diagnostic that reveals the raw API response structure before adding loop complexity, and code-agent-v1.py, the first fully working agent. The central lesson is the nested loop architecture — an outer loop that manages the conversation and an inner loop driven by stop_reason that handles tool execution — and how the messages array grows through each iteration to form the model's complete working memory.
Read the full lecture narrative
stop_reason and tool_use response structure, with stub tools.stop_reason, tool_use content blocks, and agentic loop mechanics.tool_result message formatting, the role: "user" convention, and is_error handling.