Repository navigation
Bedrock Converse Lab · Build a disruption desk and reason about the agent loop #4
akash-coded
started this conversation in
Hands-on Labs
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
TravelMind Disruption Desk: Build and Reason About an Agent
The job: a storm just cancelled flight TM482. Build an agent that helps a stranded passenger: pull their booking, find rebooking options, check the vouchers they are owed, and draft a notification. The agent presents options. It cannot rebook a seat or charge a card. That stays human-gated.
What you practice: designing an agent on paper, defending the design against the three lenses that decide every production AI system, then building it, running it, and measuring whether reality matches your prediction.
flowchart TD Q["Quality: is the answer correct and safe?"] L["Latency: how long does the passenger wait?"] C["Cost: what does it cost at disruption-scale volume?"] Q --> D{"Every design choice trades these off"} L --> D C --> DWorking rule for this exercise: quality first (a wrong rebooking strands a person), latency second (they are waiting on a cancelled flight), cost third (but a mass disruption is high volume, so cost still bites). You will set your own weights in Part 2 and defend them.
How this works
Part 1: Predict before you build
An agent is not one model call. It is a loop. The model asks for a tool, you run it, you hand the result back, it asks for another, and so on until it is done. Each loop is a fresh
conversecall that resends the entire conversation so far.flowchart LR U["Passenger message"] --> A1["converse call 1"] A1 -->|needs booking| T1["get_booking"] T1 --> A2["converse call 2 resends all"] A2 -->|needs flights| T2["find_rebooking_options"] T2 --> A3["converse call 3 resends all"] A3 -->|needs vouchers| T3["check_entitlements"] T3 --> A4["converse call 4 resends all"] A4 --> F["Final turn drafts the notification"]See "resends all" on every step. That is the single most important agentic intuition. Before you write any code, predict what it does to your token bill.
Fill Table 1A (your prediction). No calculator pressure, just reason it out. Assume the system prompt plus tool schemas is about 700 input tokens, and each turn adds about 250 tokens of history (the prior tool call plus its result):
A single non-agentic call would be about 700 input tokens, full stop.
Q1. Roughly how many times more input tokens does the 4-step agent burn versus one call? Your number: ______
Q2. If you could delete one tool round-trip, which would you cut, and why? ______
Keep these answers. You check them against real numbers in Part 5.
Part 2: Design decisions
Decision 1: pick the agent's model
Here is what you need to fill the matrix. You already know these three.
Step 1: set your weights. They must sum to 100. Justify each in one line.
Step 2: score each model 1 to 5 on each lens (5 = best). One cell per row is filled to show the idea.
Weighted total (use weights as fractions, so 50 becomes 0.5):
Your pick: __________. One-line defense: __________
Decision 2: what is a tool, and what is not
Tools FETCH data or take ACTIONS in the world. Writing prose and reasoning over data you already have is generation, which the model does for free. Drawing this line wrong is the most common beginner mistake.
flowchart TD S{"Does the step need external data or to change the world?"} S -->|yes, fetch or act| TOOL["Make it a tool"] S -->|no, it is writing or reasoning| GEN["Let the model generate it directly"]Fill Table 2A. For each step, decide tool or generation, and why.
Granularity choice. You could build one mega-tool
handle_disruption(pnr)that does everything, or three small tools. Pick one and note the trade-off. There is no single correct answer here.Decision 3: orchestration shape
flowchart LR subgraph SEQ["Sequential"] a1["tool A"] --> a2["tool B"] --> a3["tool C"] end subgraph PAR["Parallel, independent tools in one turn"] b0["one turn"] --> b1["tool A"] b0 --> b2["tool B"] endIn this flow,
find_rebooking_optionsneeds the route fromget_bookingfirst, so those are sequential. Butcheck_entitlementsonly needs delay hours and fare class, which you may know early.Q3. Which two steps could run in parallel if you restructure, and which lens does that improve (quality, latency, or cost)? __________
Decision 4: guardrails and loop control
The agent presents options. It must never rebook a seat or charge a card.
Q4. How do you guarantee the agent cannot charge a card? (Hint from the refund lab.) __________
Set two safety knobs and justify each:
MAX_TURNStemperaturePart 3: Build it (fill the blanks)
Copy this into a file called
disruption_desk.py. Fill every# TODO. The blanks are exactly the decisions you just made.Stuck on a blank? Gentle nudges, not answers:
propertiesandrequired. Two properties, both required.toolUseIdon thetoolUseblock, andoutputis what your function returned.Part 4: Run it on VS Code
disruption-desk) and open it in VS Code with File > Open Folder.python3 -m venv .venvthensource .venv/bin/activatepython -m venv .venvthen.venv\Scripts\activatepip install boto3aws configure, paste your Access Key and Secret Key, set regionus-east-1, outputjson.AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY,AWS_DEFAULT_REGION=us-east-1.us-east-1: Bedrock console > Model access. If it is off, the run will print anAccessDeniedmessage.disruption_desk.py, paste your filled skeleton, save.python disruption_desk.pyIs it working? You should see one line per turn with input and output tokens, then the final notification, then a summary with total cost and wallclock seconds.
NoCredentialsErroraws configurehereAccessDeniedExceptionValidationExceptionPart 5: Measure and reflect
The run printed real numbers. Fill Table 5A with the ACTUAL per-turn input tokens, then compare to your Part 1 guess.
The bars grow because the conversation grows. That growth is the price of autonomy.
Reflection:
Part 6: Trade-off experiments (light)
Run each, record one number, write one sentence.
Q5. After the model swap, which lens improved and which got worse? __________
Q6. After cutting a round-trip, did total input drop, and roughly by how much? __________
Stretch tasks (for the proficient)
Pick any. This is where the production skill lives.
Cascade. Run on the cheap model. Add a
confidencefield to the final output via forced structured output. If confidence is low, rerun that one case on Sonnet. Measure escalation rate and blended cost. Goal: Sonnet-grade quality at near-cheap cost.Prompt caching. The system prompt and tool schemas repeat every turn. Enable prompt caching on that prefix and measure the input-token drop across the 4 turns. Predict the savings first:
$$P_{in}^{\text{eff}} = (1-h)P_{in} + h \cdot 0.1 P_{in}$$
Parallelize. Restructure so
check_entitlementsandfind_rebooking_optionsare requested in the same turn once the booking is known. Measure the latency cut.Two-temperature design. Keep the tool and decision turns at temperature 0, then make a separate final call at a higher temperature for the notification prose only. Compare the warmth of the message.
Guarded output. Force the final options into a fixed schema (flight, voucher, action_required) so a downstream UI can render it without parsing prose.
Eval harness. Write 5 scenarios (cancelled vs delayed, FLEX vs SAVER, short vs long delay). Run all on two models. The voucher decision is rule-based, so you can score correctness automatically. Which model passes, and at what cost?
Failure injection. Make
get_bookingreturn an error for an unknown PNR. Confirm the agent asks the passenger to recheck the code instead of hallucinating a booking.The most important question on this page: should this even be an agent? Rebuild the whole thing as a SINGLE structured call: one model call, all the dummy data passed in the prompt, forced structured output. Compare cost, latency, and quality to the agent. When is the non-agentic version the right answer?
Note - The instinct to make everything agentic is the expensive instinct. Every tool round-trip is a network hop, a fresh bill, and a new failure point. The best move is to use the fewest steps that still hit your quality bar, and to know when one well-structured call beats a loop.
All reactions