ЁЯПл The SchoolтА║ЁЯУЛ AI AgentsтА║ЁЯЪз рдзрдбрд╛ 05 тАФ Guardrails: рд╕рд╣рд▓реАрдЪреЗ рдирд┐рдпрдо
ЁЯЦ╝я╕П See the drawing + lab ЁЯПа Course home ЁЯМ┐ Branch on GitHub тЬПя╕П View source
ЁЯЦ╝я╕П рдЖрдХреГрддреА рдЖрдгрд┐ labThe drawing + lab рдкреВрд░реНрдг рдкрд╛рдирд╛рд╡рд░ рдЙрдШрдбрд╛ тЖЧOpen full page тЖЧ

ЁЯЪз рдзрдбрд╛ 05 тАФ Guardrails: рд╕рд╣рд▓реАрдЪреЗ рдирд┐рдпрдо

ЁЯУН рддреБрдореНрд╣реА рдЗрдереЗ рдЖрд╣рд╛рдд: 8 рдкреИрдХреА рдзрдбрд╛ 05 ┬╖ рдорд╛рдЧреЗ: lesson-04-memory ┬╖ рдкреБрдвреЗ: lesson-06-failures


ЁЯУж рдпрд╛ рдмреНрд░рдБрдЪрдордзреНрдпреЗ рдХрд╛рдп рдЖрд╣реЗ

рдзрдбреЗ 01тАУ04, рдЖрдгрд┐ рди рдердХрдгрд╛рд▒реНрдпрд╛ loop рдЪреНрдпрд╛ рд╣рд╛рддреА рдЦрд▒реНрдпрд╛ рдХреНрд╖рдорддрд╛ рджреЗрдгреЗ рд╕реБрд░рдХреНрд╖рд┐рдд рдХрд░рдгрд╛рд░реЗ рдирд┐рдпрдо тАФ рдкреНрд░рддреНрдпреЗрдХ рдирд┐рдпрдо agent/agent.py рдордзреНрдпреЗ implement рдХреЗрд▓реЗрд▓рд╛ рдЖрд╣реЗ.

ЁЯзТ 5 рд╡рд░реНрд╖рд╛рдВрдЪреНрдпрд╛ рдореБрд▓рд╛рд▓рд╛ рд╕рдордЬрд╛рд╡рд▓реНрдпрд╛рд╕рд╛рд░рдЦреЗ

рд╢рд╛рд│рд╛ рд╡рд┐рджреНрдпрд╛рд░реНрдереНрдпрд╛рдВрдирд╛ рдХрд╛рдорд╛рдВрд╕рд╛рдареА рдмрд╛рд╣реЗрд░ рдЬрд╛рдК рджреЗрддреЗ тАФ рдкрдг рджрд╛рд░рд╛рд╡рд░ рд╕рд╣рд▓реАрдЪреЗ рдирд┐рдпрдо рдЪрд┐рдХрдЯрд╡реВрди. рд╣реЗ рдЖрдкрд▓реЗ рдирд┐рдпрдо, рдЖрдгрд┐ рдкреНрд░рддреНрдпреЗрдХ code рдордзреНрдпреЗ рдХреБрдареЗ рдЖрд╣реЗ:

  1. ЁЯХР рдЬреЗрд╡рдгрд╛рдкрд░реНрдпрдВрдд рдкрд░рдд рдпрд╛ (MAX_STEPS = 8): рдХрд╛рдо рдХрд╕реЗрд╣реА рдЪрд╛рд▓реЛ, N steps рдирдВрддрд░ рддреБрдореНрд╣реА рдерд╛рдВрдмрддрд╛. рдХреЛрдгрддрд╛рд╣реА рд╡рд┐рджреНрдпрд╛рд░реНрдереА рдХрд╛рдпрдо рднрдЯрдХрдд рдирд╛рд╣реА; рдХреЛрдгрддрд╛рд╣реА loop рдПрдХрд╛ рд░рд╛рддреНрд░реАрдд рдорд╣рд┐рдиреНрдпрд╛рднрд░рд╛рдЪреЗ tokens рдЬрд╛рд│рдд рдирд╛рд╣реА. рдорд░реНрдпрд╛рджрд╛ рдЧрд╛рдард▓реА рдХреА рдЕрднрд┐рдорд╛рдирд╛рдиреЗ "ЁЯЫС guardrail, not a bug" рдЫрд╛рдкрд▓реЗ рдЬрд╛рддреЗ тАФ рдХрд╛рд░рдг рддреБрдореНрд╣реА design рдХреЗрд▓реЗрд▓рд╛ рдерд╛рдВрдмрд╛ рд╣реЗрдЪ рдпрд╢ рдЖрд╣реЗ.
  2. ЁЯЪз рдХрд╛рдпрдордЪреНрдпрд╛ marker рдиреЗ рдХрд░рд╛рдпрдЪреНрдпрд╛ рдХреГрддреАрдВрд╕рд╛рдареА рд╢рд┐рдХреНрд╖рд┐рдХрд╛ рд▓рд╛рдЧрддреЗ (write gate): "writes": True рдЕрд╕рд╛ flag рдЕрд╕рд▓реЗрд▓реА tools рдЪрд╛рд▓рдгреНрдпрд╛рдЖрдзреА рдорд╛рдгрд╕рд╛рдЪреНрдпрд╛ [y/N] рд╕рд╛рдареА рдерд╛рдВрдмрддрд╛рдд. рд╡рд╛рдЪрди рд╡рд╛рд╣рддреЗ; рд▓рд┐рд╣рд┐рдгреЗ рдерд╛рдВрдмрддреЗ. рдПрдХ if statement тАФ рдмрд╣реБрдзрд╛ file рдордзрд▓реА рд╕рд░реНрд╡рд╛рдд рдореМрд▓реНрдпрд╡рд╛рди рдУрд│.
  3. ЁЯФТ рдлрд╕рд╡рддрд╛ рди рдпреЗрдгрд╛рд░реА tools (_safe_eval): tool рдЪреЗ inputs рдЕрд╡рд┐рд╢реНрд╡рд╕рдиреАрдп text рд╡рд╛рдЪрдгрд╛рд▒реНрдпрд╛, рд╕рд╣рдЬ рдкрдЯрд╡рддрд╛ рдпреЗрдгрд╛рд▒реНрдпрд╛ model рдХрдбреВрди рдпреЗрддрд╛рдд тАФ рдореНрд╣рдгреВрди calculator eval() рдРрд╡рдЬреА AST allowlist parse рдХрд░рддреЛ. caller рдЧреЛрдВрдзрд│рд▓реЗрд▓рд╛ рдЕрд╕реВ рд╢рдХрддреЛ рдЕрд╕реЗ рдЧреГрд╣реАрдд рдзрд░реВрди tools рдмрдирд╡рд╛, рдХрд╛рд░рдг рдХрдзреА рдХрдзреА рддреЛ рдЕрд╕рддреЛрдЪ (рдзрдбрд╛ 06).
  4. ЁЯз╛ рдкреНрд░рддреНрдпреЗрдХ step рдЪреА рдиреЛрдВрдж (рдЫрд╛рдкрд▓реЗрд▓рд╛ log): think, act, observe тАФ рдХреНрд░рдорд╛рдВрдХрд┐рдд, рджрд┐рд╕рдгрд╛рд░реЗ, рдкреБрдиреНрд╣рд╛ рдЪрд╛рд▓рд╡рддрд╛ рдпреЗрдгрд╛рд░реЗ. рдХрд╛рд╣реА рдЪреБрдХрд▓реЗ рдХреА log "рдХрд╛рдп рдШрдбрд▓реЗ?" рдпрд╛рдЪреЗ рдЙрддреНрддрд░ рджреЗрддреЛ (CloudTrail рдЪреА рд╕рд╡рдп тАФ AWS L06).

рдЖрдгрд┐ production рддреНрдпрд╛рд╡рд░ рдЬреЛрдбрддреЗ рддреЗ рдирд┐рдпрдо: budgets (tokens/рдкреИрд╕реЗ/рд╡реЗрд│), sandboxes (containers! тАФ Docker course рдЕрдЪрд╛рдирдХ рдкрд░рдд рдпреЗрддреЛ ЁЯН▒), scoped credentials (agent рдЪреА tools least-privilege рдЪреНрдпрд╛ рдЯреЛрдкреНрдпрд╛ рдШрд╛рд▓рддрд╛рдд, AWS L03тАУ05), рдЖрдгрд┐ рдорд╛рдгрд╕рд╛рдВрдирд╛ рдиреЗрд╣рдореА рдкреЛрд╣реЛрдЪрддрд╛ рдпреЗрддреАрд▓ рдЕрд╕реЗ kill switches.

ЁЯЧ║я╕П рдЖрдХреГрддреА

flowchart TB
    agent["ЁЯФБ the loop wants to run forever,<br/>touch everything, trust every input"]
    g1["ЁЯХР MAX_STEPS + budgets<br/>bounded loops, bounded bills"]
    g2["ЁЯЪз write gates<br/>reads flow ┬╖ writes wait for a human"]
    g3["ЁЯФТ hardened tools<br/>allowlists, not eval() ┬╖ least-privilege creds"]
    g4["ЁЯз╛ full logs<br/>every think/act/observe, replayable"]
    g5["ЁЯУж sandboxes<br/>blast radius = one container"]
    agent --> g1
    agent --> g2
    agent --> g3
    agent --> g4
    agent --> g5

тЭУ рдХрд╛рдп

ЁЯдФ рдХрд╛

рдзрдбрд╛ 01 рдиреЗ рдкрд░рд┐рдгрд╛рдорд╛рдВрдЪреЗ рд╡рдЪрди рджрд┐рд▓реЗ; рдкрд░рд┐рдгрд╛рдо рдореНрд╣рдгрдЬреЗ рдЬрдЧрд╛рд▓рд╛ рд╣рд╛рдд рд▓рд╛рд╡рдгреЗ; рдЬрдЧрд╛рд▓рд╛ рд╣рд╛рдд рд▓рд╛рд╡рдгреЗ рдореНрд╣рдгрдЬреЗ failure modes рдЪреЗ рдкрд░рд┐рдгрд╛рдо рднреЛрдЧрд╛рд╡реЗ рд▓рд╛рдЧрддрд╛рдд. "рдЫрд╛рди demo" рдЖрдгрд┐ "deploy рдХреЗрд▓реЗрд▓рд╛ agent" рдпрд╛рдВрддрд▓рд╛ рдлрд░рдХ рдЪрд╛рдВрдЧрд▓реЗ model рдирд╛рд╣реА тАФ рддреЛ рдЖрд╣реЗ рд╣реЗ рдкрд╛рдЪ рдХрдВрдЯрд╛рд│рд╡рд╛рдгреЗ рдирд┐рдпрдо, рдЕрд╕реНрддрд┐рддреНрд╡рд╛рдд рдЕрд╕рд▓реЗрд▓реЗ рдЖрдгрд┐ test рдХреЗрд▓реЗрд▓реЗ. рдХрдВрдЯрд╛рд│рд╡рд╛рдгреЗ рдЕрд╕рдгреЗ рд╣реЗрдЪ рдзреНрдпреЗрдп (k8s course рдЪреЗ рдмреНрд░реАрджрд╡рд╛рдХреНрдп рдкрд░рдд рдпреЗрддреЗ).

ЁЯзк рдХрд░реВрди рдкрд╛рд╣рд╛ тАФ рдкреНрд░рддреНрдпреЗрдХ рдирд┐рдпрдо рдЕрдиреБрднрд╡рд╛

# rule 2 тАФ the gate, live:
python3 agent/agent.py            # at the GATE prompt, answer N
# тЖТ "denied by the human тАФ pick another way": the loop ADAPTS, goal continues

# rule 1 тАФ the curfew:
python3 agent/agent.py --drive    # just keep calling lookup, 8 times
# тЖТ "ЁЯЫС stopped: hit MAX_STEPS" тАФ the wandering student comes home

# rule 3 тАФ the hardened tool:
# your move> calc {"expression": "__import__('os').system('echo pwned')"}
# тЖТ tool error тАФ the allowlist held ЁЯФТ

тПня╕П рдкреБрдвреЗ

рдЪреБрдХрддреЗ рддреЗрд╡реНрд╣рд╛ рдкреНрд░рддреНрдпрдХреНрд╖рд╛рдд рдХрд╛рдп рдЪреБрдХрддреЗ: failure modes тАФ compounding errors, loops, injection тАФ рдЖрдгрд┐ evals рддреНрдпрд╛рдВрдирд╛ рдХрд╕реЗ рдкрдХрдбрддрд╛рдд.

git checkout lesson-06-failures

ЁЯЪз Lesson 05 тАФ Guardrails: the field-trip rules

ЁЯУН You are here: Lesson 05 of 8 ┬╖ Previous: lesson-04-memory ┬╖ Next: lesson-06-failures


ЁЯУж What's in this branch

Lessons 01тАУ04, plus the rules that make it safe to hand a tireless loop real capabilities тАФ every one implemented in agent/agent.py.

ЁЯзТ Explain like I'm 5

The school lets students run errands тАФ WITH field-trip rules taped to the door. Ours, and where each lives in the code:

  1. ЁЯХР Be back by lunch (MAX_STEPS = 8): however the errand goes, you stop after N steps. No student wanders forever; no loop burns a month of tokens overnight. Hitting the limit prints a proud "ЁЯЫС guardrail, not a bug" тАФ because a stop you designed IS success.
  2. ЁЯЪз Permanent-marker actions need the teacher (the write gate): tools flagged "writes": True pause for a human [y/N] before running. Reads flow; writes wait. One if statement тАФ arguably the most valuable line in the file.
  3. ЁЯФТ Tools that can't be tricked (_safe_eval): tool inputs come from a persuadable model reading untrusted text тАФ so the calculator parses an AST allowlist instead of eval(). Build tools as if the caller might be confused, because sometimes it is (lesson 06).
  4. ЁЯз╛ Every step on the record (the printed log): think, act, observe тАФ numbered, visible, replayable. When something goes wrong, the log answers "what happened?" (CloudTrail instincts тАФ AWS L06).

And the rules production adds on top: budgets (tokens/money/time), sandboxes (containers! тАФ the Docker course suddenly reappears ЁЯН▒), scoped credentials (an agent's tools wear least-privilege hats, AWS L03тАУ05), and kill switches humans can always reach.

ЁЯЧ║я╕П Diagram

flowchart TB
    agent["ЁЯФБ the loop wants to run forever,<br/>touch everything, trust every input"]
    g1["ЁЯХР MAX_STEPS + budgets<br/>bounded loops, bounded bills"]
    g2["ЁЯЪз write gates<br/>reads flow ┬╖ writes wait for a human"]
    g3["ЁЯФТ hardened tools<br/>allowlists, not eval() ┬╖ least-privilege creds"]
    g4["ЁЯз╛ full logs<br/>every think/act/observe, replayable"]
    g5["ЁЯУж sandboxes<br/>blast radius = one container"]
    agent --> g1
    agent --> g2
    agent --> g3
    agent --> g4
    agent --> g5

тЭУ What

ЁЯдФ Why

Lesson 01 promised outcomes; outcomes mean touching the world; touching the world means the failure modes have consequences. The difference between "cool demo" and "deployed agent" is not a better model тАФ it's these five boring rules, present and tested. Boring is the goal (the k8s course's motto returns).

ЁЯзк Try it тАФ feel each rule

# rule 2 тАФ the gate, live:
python3 agent/agent.py            # at the GATE prompt, answer N
# тЖТ "denied by the human тАФ pick another way": the loop ADAPTS, goal continues

# rule 1 тАФ the curfew:
python3 agent/agent.py --drive    # just keep calling lookup, 8 times
# тЖТ "ЁЯЫС stopped: hit MAX_STEPS" тАФ the wandering student comes home

# rule 3 тАФ the hardened tool:
# your move> calc {"expression": "__import__('os').system('echo pwned')"}
# тЖТ tool error тАФ the allowlist held ЁЯФТ

тПня╕П Next

What actually goes wrong when it goes wrong: failure modes тАФ compounding errors, loops, injection тАФ and how evals catch them.

git checkout lesson-06-failures
тЖР PreviousmemoryNext тЖТfailures

This page is the lesson's README from the lesson-05-guardrails branch, shown here so the whole School stays on one site. Code files open on GitHub at the same branch.