--- theme: seriph title: "Lesson 19 — When Should an Agent Think in Code Instead of Words?" info: "English video course for AI Agents in Depth" author: Bojie Li transition: slide-left mdc: true lineNumbers: false monaco: false aspectRatio: 16/9 canvasWidth: 980 layout: cover class: cover ---
Build · Chapter 5 · Coding Agents
# When Should an Agent Think in Code Instead of Words?

Math, logic, and deterministic business constraints

Lesson 19 of 42 · 19 minutes · Code as a Thinking Tool; Code as a Constraint for Business Rules
--- # Why this problem matters

Calculation

Delegate exact arithmetic to a runtime.

Logic

Translate constraints into a solver.

Policy

Use server-side ground truth for irreversible decisions.

--- # Three ideas to keep in view

Formalization

Convert a verbal problem into variables and constraints

Execution feedback

The environment returns exact results or errors

Three-tier rule safety

Prompt → checklist → server gate

--- # The book's visual model Agent bootstrapping loop
Agent bootstrapping loop
--- # Language-only vs. Code-assisted

Language-only

Code-assisted

Use language to interpret and code to guarantee.
--- # Never trust self-reported policy facts ~~~python order = db.get(order_id) now = server_clock.now() eligible = policy.check(order, now) if not eligible: return reject_with_reason(order) ~~~ --- # Test the claim
5-12 min

Self-check code-assisted math

Observe: Exact sandbox execution and scoring against truth

5-22 min

Solve logic as constraints

Observe: Variables, biconditional constraints, and verified solutions

5-32 min

Run codified-rule self-tests

Observe: Checklist guidance versus server-side enforcement

Demo budget: 6 minutes · one contiguous terminal block
--- class: course-terminal ---
Live demo
# Switching to the terminal ~~~bash $ uv run python chapter5/code-for-math/demo.py --selfcheck $ uv run python chapter5/code-for-logic/demo.py --mode solver --min-people 4 $ uv run python chapter5/small-model-codified-rules/demo.py --selftest ~~~
Run the command(s), narrate decisions, and point to the observation—not just the output.
--- # What the evidence supports

Finding 1

Code replaces fragile mental computation with exact environmental feedback.

Finding 2

Constraint solvers reveal whether a verbal interpretation is internally consistent.

Finding 3

Critical rules must obtain facts from sources the model cannot forge.

--- # Boundary → design rule
Formalization can encode the wrong problem perfectly; interpretation still needs review.
Use the model to translate intent, code to enforce invariants, and tests to verify the translation.
--- # Continue the experiment
Full math comparison chapter5/code-for-math/ Full logic comparison chapter5/code-for-logic/ Codified-rules campaign chapter5/small-model-codified-rules/
--- layout: center class: text-center ---
Pause and apply
# Your turn
Which rule in your product is too important to exist only as natural language?
--- layout: center class: text-center ---
Next · Lesson 20
Generate visual artifacts by writing code, rendering pixels, and reviewing the result.