Human-in-the-Loop Workflows
Independent educational resource, not an official Anthropic course. Facts about the API, prices and limits were checked against Anthropic's documentation on 15 November 2026 and the code was run against a local mock of the API, not the live service. Check the linked official pages before relying on a number, and test with your own key.
An agent can draft, look up and propose. Whether it should be allowed to send, pay or delete is a different question, and for most business uses the honest answer is "not alone". Human-in-the-loop means a person approves certain steps. Done well it's not a slow, annoying checkpoint: it's what lets you give an agent useful tools at all. This lesson shows how to pause a run, save it, ask a person and resume exactly where it left off.
What you'll learn
- Which steps deserve a human decision.
- How pausing works: the loop stops and its state becomes plain JSON.
- Resuming with an approval or a rejection, and what the model is told.
- Designing the approval screen so people don't rubber-stamp.
Before you start
Which steps need a person
Ask of each tool: if this is wrong, can we undo it? Money moving, messages sent to customers, data deleted or changed, access granted: those need a decision. Lookups and drafts usually don't. Two other triggers are worth having: low confidence (the agent says it isn't sure) and unusual size (a refund ten times the normal amount). The approval isn't a punishment for the agent. It's the price of a permission you'd otherwise never grant.
How pausing works
When the model asks for a tool marked write, our loop doesn't run it. It runs any read-only calls in the same turn, stores their results, and returns a Run with status paused, the list of pending calls and a state dictionary. The state is plain JSON: the messages so far, the counters, and what's waiting. There's no live Python object to keep alive, so you can store it in a database row and the worker can exit.
run = agent.run(email_text)
if run.status == "paused":
db.save(ticket_id, state=json.dumps(run.state), pending=run.pending) # then the worker is free
Later, whenever a person decides (a minute or a day later), you load the state and resume:
state = json.loads(db.load(ticket_id).state)
run = agent.resume(state, {pending_id: True}) # or False to reject
The resume code is in the Agent class from lesson 6.1. What it does, all covered by tests:
- It refuses to continue unless there's a decision for every pending item.
- On approval it runs the tool, and sends back the results in the model's original order. The API expects one result per tool request, so we sort them back into order.
- On rejection it doesn't run anything. It sends an error result saying a person declined and that the action shouldn't be retried, so the model can explain it to the customer.
- It works on a copy, so the saved state isn't modified. That also means nothing stops you resuming the same state twice, so your own code must guard against it: check the ticket's status and mark it as resumed in the same database transaction.
Where the approval lives
The approval must happen outside the model's conversation. If the model asks "may I proceed?" in text, then text from a customer or a document might talk it into saying yes. Our approval is a function call from your own code, tied to a logged-in person. The model never sees a button it could press.
Check that the decision is still valid
Time passes between pause and approval. Before running an approved tool, re-check the facts it depends on: has the order since been refunded another way, did the customer cancel? Our capstone requires the ticket to be in the awaiting_approval state and raises an error otherwise, so a stale or duplicate approval can't run twice.
Designing the approval screen
People approve what they can see, quickly. So show:
- the proposed action in plain words, with the exact values (order id, amount);
- why the agent proposed it, in a sentence or two;
- the facts it relied on, such as the order status it looked up;
- two clear buttons, and an optional reason for rejecting.
If approvals pile up, people click through without reading. Keep the number of requests low by sending only the risky steps to a person, and record who approved what and when.
Testing it
The tests cover: a write tool pauses and nothing runs until approved; state survives json.dumps and json.loads; results come back in the model's order; a rejection reaches the model as an error; resuming without a decision fails; and the original saved state is unchanged after a resume. We also ran a full pause and resume through the real Python SDK against a mock of the API.
Real-world use case (illustrative scenario)
This scenario is an invented example to show how the ideas fit together. It is not a case study and its details are not measured results.
An HR assistant answers employee questions and can propose a change to someone's working hours. Questions never pause. A change request does: the manager gets a message showing the old and new hours and the employee's reason. Two days later the manager approves, the state is loaded and the change is made. If the manager rejects it, the assistant is told and writes a polite note to the employee. Nobody had to keep a process running for two days.
Mistakes to avoid
- Letting the model ask for approval in the chat.
- Keeping the paused state only in memory.
- Approving once, then running the action many times.
- Sending everything for approval so people stop reading.
- Not recording who approved.
Security notes
The approver should be authenticated and authorised for this specific action. Don't trust state that comes from a browser: load it from your database by id, so a user can't edit the saved messages before they're resumed. Store the audit trail where the agent cannot write to it.
Cost and speed
A paused run costs nothing while it waits. When it resumes, the next request re-sends the conversation, so long gaps cost the same as short ones, though if you use prompt caching the cache will probably have expired. The latency to the customer is the human's response time, so set expectations in the interface.
Key takeaways
- Put a person in front of irreversible, high-value or uncertain actions.
- Pause by returning JSON state; resume by passing decisions back.
- Approve outside the conversation, and re-check that the decision is still valid.
- Make approvals easy to read, and keep them few.
Lesson recap: pause with JSON state, let a person decide elsewhere, resume, and keep approvals few.
Quick quiz
- What kinds of actions should wait for a person?
- Why is the paused state plain JSON?
- What does the model see when a person rejects an action?
- Why must approval happen outside the model's conversation?
- Why re-check facts before running an approved action?
Answers and explanations
- Actions that are hard to undo or high-impact: payments, customer messages, deletions, access changes, and cases where the agent is unsure.
- So it can be saved to a database and loaded later by any worker, without keeping a process alive.
- An error result saying a person declined and not to retry, so it can explain this to the user.
- Text in the conversation can come from customers or documents, and could talk the model into approving itself. A call from your own authenticated code can't be forged that way.
- Time may have passed and the situation changed, such as the order already being refunded.
Official references and further reading
Related tutorials