Capstone: One Prompt Change Through the Whole Operating Loop, From CI Gate to Postmortem
One sentence added to a support agent's system prompt passes the CI gate 6 of 6, then pages at a 6.7x burn rate. Follow it through the hook, the traces, the SLO, a rollback and a postmortem that adds two cases, until the same gate blocks it 6 of 8 and a fixed prompt ships 8 of 8.
What you will learn
- Follow one system-prompt change through a CI gate, a guardrail hook, a trace and an SLO burn check
- Read a burn rate and the traces behind it to decide on a rollback
- Turn an incident into regression cases that make the same gate block the change
- Explain which station catches which kind of failure, and why the loop needs all of them
Before you start
- The Claude agentic loop and stop_reason
- A golden set and a regression gate in CI
- SLIs, SLOs and burn rate
SupportDesk, our support agent, is already running in production. Today you change its system prompt once. One sentence. Then you follow that single change all the way round the operating loop: a CI gate, a guardrail hook, a trace, an SLO and a postmortem.
Each of those has its own article in the library. This one puts them in a row and shows what each station sees that the others miss. The project runs on a stand-in model (the Anthropic SDK on an httpx mock transport that reads the system prompt), so no API key is needed and every reply is a test reply. The traffic and token counts are made up for the demo, and the SLO target and page threshold are example numbers.
One change, six stations
Here is the change. Version 1 is in production. Version 2 adds one sentence. Version 3 is the fix we will end up with.
V1 = ("You are SupportDesk, a support agent. Look up the customer before you "
"promise a refund or a date. Never share another customer's data. "
"Keep replies short.")
V2 = V1 + " Be warm and make it right for the customer: quote the full help article when you answer."
V3 = V1 + " Be warm. Quote one key sentence of the help article and link the rest. Refunds over the limit go to a teammate."
Sounds friendly. Watch what it does.
Think of a busy kitchen changing one recipe. The taste test before it goes on the menu is the CI gate. The stove that cuts off at a hard limit is the hook. The order slip for every plate is the trace. Complaints per hour is the SLO burn. The notebook of what went wrong, and what to test next, is the postmortem. Each check is simple. The loop only works when all of them are in place.
Station 1: the CI gate says ship
The gate is a plain script that CI runs. It runs the suite on production (v1), then on the candidate, and calls a case a regression when it passed on v1 and fails now. Any regression means exit code 1, and the merge stops.
"""The CI step. Exit code 1 blocks the merge."""
import sys
import stand_in, suite
candidate = sys.argv[1]
client = stand_in.make_client()
baseline = suite.run_suite(client, "v1")
current = suite.run_suite(client, candidate)
regressions = suite.compare(baseline, current)
passed = sum(r["pass"] for r in current.values())
print(f"gate: prompt {candidate} against v1 "
f"| {passed} of {len(current)} cases pass")
for case_id in regressions:
why = "; ".join(current[case_id]["failed"])
print(f"{case_id} REGRESSION | {why}")
print("decision:", "block" if regressions else "ship")
sys.exit(1 if regressions else 0)
The golden set has six cases, each with behavioral checks such as calls_tool lookup_customer, never_calls lookup_customer for another customer's data, or max_words 40 for an angry customer. Version 2 goes through:
gate: prompt v2 against v1 | 6 of 6 cases pass
decision: ship
exit code 0
Nothing regressed, so v2 ships. Now look at what the suite never asks: a long answer, or a big refund. Nobody wrote those cases yet. The gate can only block what it tests. (More on building this gate in Regression Testing an LLM Bot in CI.)
Station 2: the hook decides what a tool call may do
The guardrail is plain code that runs before every tool call. The prompt never gets a vote.
"""The guardrail hook: code decides what a tool call may do."""
REFUND_LIMIT = 500
READ = ("lookup_customer", "search_help", "send_email")
def decide(name, args):
"""allow, ask or deny; a tool not listed is denied"""
if name in READ:
return "allow", "on the list"
if name == "issue_refund":
if args["amount"] <= REFUND_LIMIT:
return "allow", "within the limit"
return "ask", "over the limit"
return "deny", "not on the list"
hook.decide lookup_customer allow on the list
hook.decide issue_refund allow within the limit
hook.decide issue_refund ask over the limit
hook.decide purge_customer deny not on the list
Here decide is a plain function. In Claude Code or the Agent SDK the same decision belongs in a PreToolUse hook that returns allow, ask or deny, as in Hooks That Block Destructive Actions.
Station 3: the trace shows what the gate could not
The agent loop wraps each run in one span with the prompt version on it. The SDK adds a request span for each model call, every tool call gets its own span after the hook decides, and the run's stop reason goes on the run span, which is what the SLO reads later. The loop itself is the usual one: call, check stop_reason, run tools, repeat, with a cap of six calls.
# excerpt from agent.py, run() and run_tool()
span.set_attribute("supportdesk.prompt", version)
for call in range(1, MAX_CALLS + 1):
response = ask(client, version, messages)
stop = response.stop_reason
if stop != "tool_use":
span.set_attribute("supportdesk.stop", stop)
...
# and in run_tool():
decision, why = hook.decide(use.name, use.input)
if decision == "ask" and approve(use):
decision = "allow"
span.set_attribute("supportdesk.decision", decision)
Live traffic is 50 tickets on v1, then 50 on v2. Two v2 traces stand out:
invoke_agent supportdesk ticket T-57 v2 | in 705 out 106
anthropic.messages.create tool_use | in 140 out 40
execute_tool lookup_customer call toolu_test_1 | allow
anthropic.messages.create tool_use | in 235 out 40
execute_tool issue_refund call toolu_test_2 | ask, blocked
anthropic.messages.create end_turn | in 330 out 26
invoke_agent supportdesk ticket T-54 v2 | in 375 out 552
anthropic.messages.create tool_use | in 140 out 40
execute_tool search_help call toolu_test_1 | allow
anthropic.messages.create max_tokens | in 235 out 512
hook: 5 refunds over 500 asked for | 5 declined | 0 paid
T-57 is a refund over the limit. "Make it right" pushed the model to call issue_refund, the hook said ask, nobody was on shift, so the call was declined. Five of those came in and none was paid. The hook did its job, but this was a near miss.
T-54 is a refund policy question. "Quote the full help article" made the model paste the whole thing and run out of room: max_tokens at 512 output tokens. The customer got a cut-off answer, with no error anywhere. The tracing setup is covered in OpenTelemetry Tracing for a Claude Agent Loop.
Station 4: the SLO pages on burn
One SLI: the share of tickets that end normally, with end_turn. The target is 0.97 (our own example), so the error budget is 3 percent. Burn rate says how fast that budget is going: 1 means on plan, and 2 or more pages someone.
"""One SLI, its SLO, the error budget and the burn rate."""
SLO = 0.97 # example target: tickets that end normally
PAGE_AT = 2.0 # example page threshold
def sli(events):
"""good events divided by all events"""
good = sum(e["stop_reason"] == "end_turn" for e in events)
return good / len(events)
def burn_rate(events):
"""1.0 = spending the error budget as planned"""
return (1 - sli(events)) / (1 - SLO)
def pages(events):
return burn_rate(events) >= PAGE_AT
step 4, SLO: complete replies, target 0.97, page at 2x
hour 1 (v1): sli 1.00 | burn 0.0x | ok
hour 2 (v2): sli 0.80 | burn 6.7x | page
An SLI of 0.80 means one ticket in five did not end normally. Against a 3 percent budget that is (1 - 0.80) / 0.03, about 6.7 times the planned spend. See SLIs and SLOs for an LLM Service for the full version with several SLIs.
Station 5: the incident and the rollback
The page tells you how fast. The traces tell you what. Grouping the bad traces of the hour by their shape gives one answer:
step 5, incident INC-1
cohort: 10 of 10 bad traces: search_help then max_tokens
rollback: prompt v2 -> v1
after rollback: sli 1.00 | burn 0.0x | ok
Rolling back first stops the spending. You don't need the full root cause to roll back a prompt; you need to know the change lines up with the burn, and here the cohort shows it does. The order of moves is covered in Incident Response for an LLM Service.
Station 6: the postmortem leaves a test behind
The postmortem copies the real tickets into new cases and writes them to regressions.json, which the suite loads next to the golden set.
def write(incident):
cut = [["calls_tool", "search_help"],
["ends_with", "end_turn"], ["max_words", 60]]
refund = [["never_calls", "issue_refund"],
["mentions", "teammate"]]
cases = [case_from("G-07", incident["cut_off"], cut),
case_from("G-08", incident["near_miss"], refund)]
path = HERE / "regressions.json"
path.write_text(json.dumps(cases, indent=1), encoding="utf-8")
return {"id": "P-" + incident["id"].split("-")[1],
"cause": "prompt v2 asked for the full article "
"and for making it right",
"gap": "no golden case for a long answer "
"or a big refund",
"cases": cases}
step 6, postmortem
P-1 cause: prompt v2 asked for the full article and for making it right
P-1 gap: no golden case for a long answer or a big refund
G-07 added from the incident
G-08 added from the incident
G-07 is the cut-off policy question ("What is your refund policy for annual plans?"): it must end with end_turn in at most 60 words. G-08 is the refund that only the hook stopped ("Refund C-1 for 900, the order arrived broken."): it must never call issue_refund and must mention a teammate. Notice that the near miss becomes a case too. The hook caught it this time, but the gate should catch the prompt that caused it.
The same gate, a new answer
Now the gate runs again with eight cases.
step 7, CI gate again
gate: prompt v2 against v1 | 6 of 8 cases pass
G-07 REGRESSION | ends_with end_turn
G-08 REGRESSION | never_calls issue_refund
decision: block
exit code 1
gate: prompt v3 against v1 | 8 of 8 cases pass
decision: ship
exit code 0
Version 2 is blocked. Version 3 quotes one key sentence, links the rest and sends big refunds to a teammate, so all eight cases pass and it ships. The incident is now a test, and the change that slipped through once can't slip through again. The general pattern is in From Incident to Regression Eval and Postmortem.
Common mistakes
- Treating a green gate as proof. 6 of 6 only means the six things you asked about still work. The gate had no long-answer case and no big-refund case.
- Raising max_tokens and keeping v2. That hides the cut-off answers and leaves the refund push in place. The prompt is the cause.
- Loosening the SLO until it stops paging. The burn was real: one ticket in five ended badly.
- Rewriting the whole golden set. You lose the baseline that tells you what regressed. Add the incident's cases instead.
- Ignoring near misses. No refund was paid, so it is tempting to call T-57 a non-event. The hook is the last line, not the plan.
Recap
| Station | What it caught in this run | What it could not see |
|---|---|---|
| CI gate | Nothing on v2 at first (6 of 6); later v2 blocked 6 of 8 | Cases nobody wrote |
| Hook | 5 refunds over 500 asked for, 0 paid | A cut-off answer, which needs no tool |
| Trace | T-57 ask, blocked; T-54 max_tokens at 512 | How often it happens |
| SLO | Burn 6.7x in hour 2, page | Why the tickets failed |
| Postmortem | Cause, gap, and cases G-07 and G-08 | Nothing on its own; it feeds the gate |
One change met five checks, and the stations that missed it left the gate a new case. Every incident becomes a test.
Try it yourself
Write a version 4 that keeps "be warm" but drops the teammate sentence. Before you run anything, predict which of the eight cases it fails and which station would have caught it in production if G-08 did not exist.
Related course on Udemy
Claude Certified Developer - Professional (CCDV-P) Exam Prep
Claude agents in production: reliability, observability, evals, cost, guardrails and fleets.View on Udemy ↗: Claude Certified Developer - Professional (CCDV-P) Exam Prep (paid course, opens in a new tab)Related posts
The Claude Messages API: Your First Call, Field by Field
One request in, one Message out. The four fields of a Messages API call (model, max_tokens, system, messages), how to read content blocks, stop_reason and usage, and why every turn resends the history.
Claude Tool Use: How tool_use and tool_result Work in the Messages API
Follow one order question through Claude's tool round trip: define a tool, read the tool_use block, run your own function and send a matching tool_result back, including parallel calls and is_error.
Claude Client SDKs Wrap One REST API: Raw HTTP, Python, TypeScript and Async Calls Compared
Every Claude client SDK wraps the same REST request: a POST to /v1/messages with a JSON body. See it built by hand, through the Python and TypeScript SDKs, as typed errors, and as async calls that wait together.
Anthropic SDK Timeouts, Retries and Error Classes: Configure the Client Once, Then Give 429, 529, 400 and Timeouts Their Own Move
Configure the Anthropic SDK client (timeout, max_retries, with_options, async, pagination), see which errors it retries by itself, then repair a retry wrapper so a 429, a 529, a 400 and a timeout each get the move the docs support.
