From tool request to verified action
Trace a support case through a tool loop, enforce account checks, and hand off exceptions.
Follow one support request
A member asks why a renewal was charged twice. Before acting, the assistant must identify the correct account, read both charge records, and distinguish a duplicate capture from a pending authorization. The host application owns the conversation and the billing tools.
1.1 · Read the response before choosing the next step
Send the available tool definitions and conversation to the Messages API. Save the complete assistant message, including text and tool-use blocks. When stop_reason is tool_use, execute each authorized call, then send a user message containing a tool_result for each matching tool_use_id. Claude can now decide whether it needs another lookup. If the reason is end_turn, return the answer. Text such as “I have finished checking” can appear alongside an outstanding tool request.
assistant: text + tool_use(id=A, read_charge)
user: tool_result(tool_use_id=A, capture_status=pending)
assistant: text + end_turn
Handle other stops explicitly. A token limit can leave an incomplete response. Record the interruption and recover or hand off. A time or cost limit bounds a runaway loop. Reaching that limit is an incomplete run, even when the last message sounds final.
Finish a turn with two outstanding reads
The assistant writes “I can finish this now” and requests charge_lookup with ID C7 and receipt_lookup with ID R2. The response stops with tool_use. R2 finishes first. C7 times out. Write the next message and decide whether the host can mark the support case resolved.
Reconstruct the exchange
Keep the assistant’s entire message. Send the receipt result under R2 and a tool error under C7 in the next user message. Putting the receipt under C7 would make a real receipt look like evidence about a charge. The model can use both outcomes to choose another permitted lookup, ask a question, or explain the gap. The timeline records that charge verification is incomplete.
If the next response stops at end_turn, the model has returned control. The application still checks its resolution criteria. A limit reached while an essential check is pending should yield an incomplete case. The same rule applies when the assistant produced convincing prose on the interrupted turn.
Video background · freeCodeCamp at 1:00:54 · Returning tool results to the loop
1.4 · Guard state changes and prepare a handoff
Store the verified member ID in trusted application state. A billing write must check that the charge belongs to that member, the amount is within the approved limit, and the adjustment has not already been applied. A plausible ID in a model argument cannot establish verification. Investigate independent issues such as a missing receipt and a duplicate charge in parallel under the same verified identity. Combine the outcomes into one reply.
A person picking up a case needs the member and charge references, the customer’s request, the suspected cause, the amount and currency, completed checks, and the next decision. Mark actions that still need approval so the next handler can distinguish a proposed reversal from a completed one.
Separate two requests before a financial handoff
A customer requests a missing receipt and a credit for order O8. The member was verified for account A, the receipt belongs to A, and the proposed credit target belongs to account B. The operator taking over cannot open the chat. What can finish now, and what information should travel with the unresolved part?
Draft the handoff record
Under A’s authority, finish the receipt lookup. At the write boundary compare the verified subject, order owner, requested amount, currency and policy. Refuse the credit on B. A model suggestion to reuse A’s verification cannot satisfy the ownership prerequisite. Keep the successful receipt result when preparing the reply.
The handoff should identify A and O8 using permitted references, the requested credit, the observed ownership mismatch, checks already performed, and the authorized operator’s next decision. Label the credit pending and explain that no adjustment was executed. Avoid exposing B’s unrelated details to A.
Video background · freeCodeCamp at 5:23:11 · Enforcing prerequisites before a downstream operation
1.5 · Choose a hook on the correct side of the call
Use a pre-call hook or backend check to deny an adjustment above the operator’s limit before it reaches billing. Route the case to approval. Use post-call handling to normalize results, for example converting epoch seconds and ISO strings into a common UTC field and mapping provider-specific status codes to named states. Preserve the raw value and source for audit. Post-processing arrives too late to prevent a charge.
Normalize a receipt without approving a refund
The billing adapter returns 1704067200 epoch seconds and status 7. Its documented mapping defines 7 as pending. A second adapter returns an ISO timestamp and the text “pending.” A refund proposal for $325 then reaches an operator whose cap is $300. Which transformations and enforcement decisions must happen, and when?
Place each hook
After each read, normalize the timestamps to an explicit UTC representation and the statuses to the common pending value. The epoch value in this fixture is 2024-01-01 at 00:00 UTC. Preserve the original value, units and provider so another reviewer can reproduce the conversion. An unknown numeric status should remain unresolved.
Before dispatching the refund, a pre-call check or the billing backend compares the authorized amount and currency against the cap. Route the $325 proposal to approval and record that billing did not receive it. A post-call hook that warns about the cap would run after the possible financial effect.
Video background · freeCodeCamp at 5:42:01 · Pre-call and post-call hooks
Trace exercise
The lookup completes, a proposed adjustment exceeds the limit, and the receipt service times out. Write the next two actions. The adjustment goes to approval with the verified lookup attached. The receipt lookup can be retried independently. Neither event establishes that money has moved.
Replay a tool turn locally
Run this Python 3 fixture with no API key. It represents one assistant message and two completed read attempts. Before running it, predict which ID receives the timeout and whether the host needs another model turn.
assistant = {
"stop_reason": "tool_use",
"content": [
{"type": "text", "text": "I have a receipt update."},
{"type": "tool_use", "id": "a1", "name": "read_charge"},
{"type": "tool_use", "id": "a2", "name": "read_receipt"},
],
}
# Simulated completion order differs from request order.
outcomes = {
"a2": ("receipt found", False),
"a1": ("charge lookup timed out", True),
}
def next_turn(message, finished):
if message["stop_reason"] == "end_turn":
return "finished", []
if message["stop_reason"] != "tool_use":
return "interrupted", []
results = []
for call in message["content"]:
if call["type"] != "tool_use":
continue
text, failed = finished[call["id"]]
results.append({"type": "tool_result", "tool_use_id": call["id"],
"content": text, "is_error": failed})
return "continue", results
state, results = next_turn(assistant, outcomes)
print(state)
for item in results:
print(item["tool_use_id"], item["content"], item["is_error"])
for reason in ["end_turn", "max_tokens"]:
print(reason, next_turn({"stop_reason": reason}, {})[0])
Check the output and explain the result
continue
a1 charge lookup timed out True
a2 receipt found False
end_turn finished
max_tokens interruptedSwapping the order of the two entries in outcomes should leave the result unchanged. A host records the assistant content and appends the results as one user message on the continuing path. This fixture has no outstanding read after the failures have been reported. A genuinely unfinished read would still need a result or an explicit recovery decision.
Further reading · Official CCAR-F exam guide, sample questions and practice exercises · Building effective agents
From tool request to verified action quiz
Answer 9 questions. Check each answer for an explanation.