Lesson 72.3 · 3 of 7 · 4 min read · Reviewed 24 Nov 2026
Feature flags and kill switches
By the end, you can
- Use feature flags to control AI features.
- Build kill switches for fast shutdown.
Flags for AI features
| Flag | Example |
|---|---|
| Feature on or off | The AI reply assistant for a tenant |
| Version selector | Prompt v14 or v15 for a canary slice |
| Route selector | Primary API or fallback platform |
| Limits | Max steps per agent run |
| Kill switch | Disable auto-sending; drafts only |
Flags read at request time let you change behaviour in seconds, without a deploy.
Kill switches
A kill switch must fall back to something safe:
- Auto-reply becomes draft for a human.
- AgentAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done. with write tools becomes read-only.
- AI answer becomes search results or a contact form.
Write down who may flip it (on-call engineer, support lead) and make it one action, not a ticket.
Test them
Flip each kill switch in staging and, occasionally, in production during quiet hours. A switch that hasn’t been tested may not work, or may break something else.
Clean up
Old flags pile up. Remove flags for finished rollouts so the code stays readable.
Key points
- Feature flags turn features, prompts or routes on and off without a deploy.
- A kill switch is a flag that disables a risky feature instantly, falling back to a safe path.
- Test kill switches before you need them, and make clear who may flip them.
Build and test a kill switch
Read flags at request time so a kill switch turns auto-sent replies into drafts at once, and limit who can flip it.
You will have: A handle() that drafts replies with the simulated model and obeys the kill switch, and a flip() with a permission check.
Steps
- Run the starter: the flip is ignored and all four replies are sent.
- Read FLAGS inside handle() on every request.
- Refuse a flip from anyone not listed for that flag.
- Run the test: two replies sent before the flip, two drafts after.
Done when
- The intern's flip is refused.
- After on-call flips the switch, replies go to drafts.
- The test prints sent 2, drafted 2 and PASS.
Open in the Playground No API key needed.
Starter code (Python)
# Flags read at request time, a kill switch that falls back to drafts, and a rule for who may flip it.
import anthropic
client = anthropic.Anthropic()
FLAGS = {"auto_send": True, "kill_auto_send": False}
MAY_FLIP = {"kill_auto_send": {"oncall", "support_lead"}}
sent, drafts = [], []
AUTO_SEND = FLAGS["auto_send"] and not FLAGS["kill_auto_send"] # read once at start-up: flips never reach handle()
def flip(flag, value, by):
# TODO 2: refuse (print "refused: <by> may not flip <flag>" and return False) unless by is in MAY_FLIP[flag]
FLAGS[flag] = value
print(f"{by} set {flag} = {value}")
return True
def handle(email):
reply = client.messages.create(
model="claude-haiku-5-5",
max_tokens=200,
messages=[{"role": "user", "content": f"Draft a short, polite reply to this customer email: {email}"}],
)
text = "".join(b.text for b in reply.content if b.type == "text")
# TODO 1: read FLAGS here, on every request, instead of the AUTO_SEND copy taken at start-up
if AUTO_SEND:
sent.append(text)
return "sent"
drafts.append(text) # the safe fallback: a person reviews it before it goes out
return "draft for a human"
EMAILS = ["Where is my order A-1044?", "My grinder arrived damaged.",
"Can I change my delivery address?", "Please cancel my subscription."]
print(EMAILS[0], "->", handle(EMAILS[0]))
print(EMAILS[1], "->", handle(EMAILS[1]))
flip("kill_auto_send", True, by="intern")
flip("kill_auto_send", True, by="oncall")
print(EMAILS[2], "->", handle(EMAILS[2]))
print(EMAILS[3], "->", handle(EMAILS[3]))
print(f"sent: {len(sent)}, drafted: {len(drafts)}")
print("kill switch test:", "PASS" if len(drafts) == 2 and len(sent) == 2 else "FAIL")
Show a solution
# Flags read at request time, a kill switch that falls back to drafts, and a rule for who may flip it.
import anthropic
client = anthropic.Anthropic()
FLAGS = {"auto_send": True, "kill_auto_send": False}
MAY_FLIP = {"kill_auto_send": {"oncall", "support_lead"}}
sent, drafts = [], []
def flip(flag, value, by):
if by not in MAY_FLIP.get(flag, set()):
print(f"refused: {by} may not flip {flag}")
return False
FLAGS[flag] = value
print(f"{by} set {flag} = {value}")
return True
def handle(email):
reply = client.messages.create(
model="claude-haiku-5-5",
max_tokens=200,
messages=[{"role": "user", "content": f"Draft a short, polite reply to this customer email: {email}"}],
)
text = "".join(b.text for b in reply.content if b.type == "text")
# Read the flags now, on every request, so a flip takes effect without a deploy.
if FLAGS["auto_send"] and not FLAGS["kill_auto_send"]:
sent.append(text)
return "sent"
drafts.append(text) # the safe fallback: a person reviews it before it goes out
return "draft for a human"
EMAILS = ["Where is my order A-1044?", "My grinder arrived damaged.",
"Can I change my delivery address?", "Please cancel my subscription."]
print(EMAILS[0], "->", handle(EMAILS[0]))
print(EMAILS[1], "->", handle(EMAILS[1]))
flip("kill_auto_send", True, by="intern")
flip("kill_auto_send", True, by="oncall")
print(EMAILS[2], "->", handle(EMAILS[2]))
print(EMAILS[3], "->", handle(EMAILS[3]))
print(f"sent: {len(sent)}, drafted: {len(drafts)}")
print("kill switch test:", "PASS" if len(drafts) == 2 and len(sent) == 2 else "FAIL")