Lab: Turn a Claude App Into a Reusable Template, One Pinned Model Id, No Key in Code and Evals Built From Data
The refund app ships as a template with a typed-in model id, a key in code and evals that only work for one shop. A five-check audit finds all of it; template.json, config.py, data-built evals, exact pins and a README fix it, and the same evals then pass for two customers.
What you will learn
- Audit a template for a hard-coded model id, a key in code, customer data, loose pins and missing docs
- Move every per-customer value into template.json with an optional customer override file
- Build eval cases from a customer's own orders and limit
- Pin package versions exactly and document parameters and a changelog
Before you start
- Ship a Claude app (part 3)
- Pinning model versions and upgrade evals
A maintainer is handed our refund app as a template for new customers. Before they accept it, it has to work for any shop, whoever opens it, with nobody editing code first. Right now it doesn't. This last lab of the build-along repairs three defects, pins what must be pinned, and writes it up. (In the course this lecture is an extra prep bonus, not a separate exam objective.)
Think of a shop manual copied to a new branch. If the supplier's name is written on every page, changing it means editing every page; written once on the front, the pages just point to it. The safe code copied into the manual can be read by every branch that gets a copy. And a staff test that names the first shop's regulars fails anywhere else.
The template as it shipped
"""The model call, as the template shipped it."""
from anthropic import Anthropic
client = Anthropic(api_key="sk-ant-...") # a key string typed into the code (shortened here)
def ask(question, tools):
return client.messages.create(
model="claude-sonnet-5-5",
max_tokens=300,
tools=tools,
messages=[{"role": "user", "content": question}])
Two defects in one file. The key string travels with every copy of the template, and the model id is typed into the call, so changing it means editing code. The eval file has the third defect: its docstring names Acme Shop, its cases use order ids like A-102 from one shop's data, and the hook case only works because of Acme's limit and prices. Another shop doesn't have A-102, so the case can't even start.
An audit with five checks
MODEL = re.compile(r"[\"']claude-[a-z0-9.-]+[\"']")
KEY = re.compile(r"sk-[A-Za-z0-9_-]{10,}")
CUSTOMER = re.compile(r"Acme Shop|\bA-\d{3}\b")
def model_id(root):
hit = scan(root, MODEL, ["*.py"])
if hit:
return False, f"model id typed in {hit}"
if not (root / "template.json").exists():
return False, "no template.json"
note = "no model id in code, one in template.json"
return True, note
def pins(root):
req = root / "requirements.txt"
if not req.exists():
return False, "no requirements.txt"
rows = [r for r in req.read_text().split() if r]
loose = [r for r in rows if "==" not in r]
if loose:
return False, f"not pinned: {loose[0]}"
return True, f"{len(rows)} packages pinned with =="
CHECKS = [("model id", model_id), ("secret", secret),
("customer", customer), ("pins", pins),
("docs", docs)]
check_template.py has one function per check: a quoted claude model id in code, key-shaped text (sk- and a long tail), a shop name or an order id, exact version pins, and a README with parameters and a changelog. scan() returns the first file and line that matches. On the template as it shipped:
$ python check_template.py defects
audit defects
FAIL model id: model id typed in app.py line 9
FAIL secret: key-shaped text in app.py line 4
FAIL customer: one customer's data in evals.py line 1
FAIL pins: no requirements.txt
FAIL docs: no README.md
5 checks fail
Repair 1: template.json and config.py
{
"template_version": "1.1.0",
"model": "claude-sonnet-5-5",
"customer": "Acme Shop",
"refund_limit": 500,
"orders_file": "orders.json"
}
Everything that changes between customers lives here, and nothing in it is secret. The template version tells a maintainer which one they reviewed. The model is named once: one exact id, the one the evals run on. The limit and the orders file are parameters now.
"""Everything a new customer changes lives here."""
import json
import os
import pathlib
HERE = pathlib.Path(__file__).parent
PARAMS = json.loads((HERE / "template.json").read_text())
# A customer file overrides the defaults.
OVERRIDE = os.getenv("TEMPLATE_CUSTOMER")
if OVERRIDE:
extra = pathlib.Path(OVERRIDE).read_text()
PARAMS.update(json.loads(extra))
MODEL = PARAMS["model"]
CUSTOMER = PARAMS["customer"]
LIMIT = PARAMS["refund_limit"]
ORDERS = HERE / PARAMS["orders_file"]
API_KEY = os.getenv("ANTHROPIC_API_KEY", "")
Every other file reads the model and the limit from here, so nothing else types a model id. If TEMPLATE_CUSTOMER points to a customer file, it overrides the defaults; that's how a second shop gets its own limit with no code edit. The key comes from the environment. The real key lives in a .env file that .gitignore keeps out of git, and .env.example is a blank copy to start from. A second customer file looks like this:
{
"customer": "Birch Books",
"refund_limit": 200,
"orders_file": "customers/birch_orders.json"
}
For the key side in more depth, see API keys, proxies, rotation and scope.
Repair 2: evals built from the data
def build_cases(orders, limit):
price = {i: o["total"] for i, o in orders.items()}
ids = sorted(price, key=price.get)
under = [i for i in ids if price[i] <= limit]
over = [i for i in ids if price[i] > limit]
small, big = under[-1], over[0]
return [
("refund within the limit",
f"Please refund order {small}.",
f"issue_refund {small} -> refund sent"),
("refund over the limit",
f"Please refund order {big}.",
f"issue_refund {big} -> hook denies"),
("unknown order",
"Where is order Z-000?",
"get_order Z-000 -> mcp: Error"),
]
The function needs only two facts: the customer's orders and their limit. It picks the biggest order under the limit and the smallest order over it, so no order id is typed in. Each case runs through the real loop, and we look for the expected step in the trace.
Repair 3: pin and document
anthropic==2.10.0
mcp==2.3.0
claude-agent-sdk==0.2.185
Each package is pinned with == to the exact version the lab ran, not a range. The model id in template.json is pinned in the same spirit (more in model pinning and upgrade evals). The README opens with the version, lists what template.json holds and what a customer file overrides, gives three steps for a new customer (copy a customer file, set the key, run the checks), and ends with a changelog of the fixes and pins.
One template, two customers
As in the rest of the build-along, model replies come from the scripted stand-in, so no key is needed.
$ python check_template.py .
audit .
ok model id: no model id in code, one in template.json
ok secret: no key in files, .env ignored
ok customer: no customer name or order id in code
ok pins: 3 packages pinned with ==
ok docs: README.md has parameters and changelog
all 5 checks pass
$ python evals.py
evals for Acme Shop, limit $500
pass refund within the limit
pass refund over the limit
pass unknown order
3 of 3 pass
$ $env:TEMPLATE_CUSTOMER="customers/birch.json"; python evals.py
evals for Birch Books, limit $200
pass refund within the limit
pass refund over the limit
pass unknown order
3 of 3 pass
Between the two eval runs only the customer file changed. No code did. That's the test of a template.
Recap
| Defect | Fix | Audit check |
|---|---|---|
| Model id typed in the call | One id in template.json, read through config.py | model id |
| Key in the code | ANTHROPIC_API_KEY from the environment, .env ignored | secret |
| Evals for one shop | build_cases(orders, limit) | customer |
| No pins, no docs | requirements.txt with ==, README with parameters and changelog | pins, docs |
Fix it, pin it, write it up. That closes the build-along: from one business need in part 1 to an app a maintainer can hand to the next customer.
Previous: lab: judge, retry and hook matcher.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
Claude Certified Developer - Foundations (CCDV-F) Exam Prep
Build and ship Claude apps: the API, agents, tools and MCP, prompts, evals, security and model choice.View on Udemy ↗: Claude Certified Developer - Foundations (CCDV-F) 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.