Theresa Nolan at Elm Grove Pediatrics writes in. November’s charge was $348, for 12 providers, and the clinic has 10. Can someone refund the difference? Forty minutes ago someone on the billing team already did: $58 back on that payment. Theresa doesn’t know that. Neither does the agent that picks up her ticket.
Most of what makes a billing agent hard to test sits in that moment. The job looks like three calls: read the ticket, refund, reply. Each one can succeed while the job fails. Stripe accepts a second $58 refund on the same payment, because a charge can be refunded in parts until the whole amount is back. Zendesk saves the reply on whichever of Theresa’s two open tickets the agent picks. Gmail sends the answer to her bookkeeper as a separate email unless the agent names his thread, sets two reply headers and keeps the subject. No call returns an error, so the agent’s log reads like a good day’s work.
One of the agents we wrote for this job sends an idempotency key with its refund, built from the payment’s ID. It refunded Theresa twice anyway. An idempotency key stops the same request from running twice. The refund already on the payment came from a person, through a request of its own, so the agent’s key had nothing to match. The key covers a retry. Only reading the account first covers a second decision.
Ancha, our product, sits in the comparison below next to the other replicas.
Why our idempotency key didn’t stop the second refund
The agent that refunded twice is a script: about sixty lines of Node, no model. Every run makes the same calls. It finds Theresa in Stripe, picks her $348 November payment and asks for refunds. If it sees no $58 refund that succeeded, it creates one. The refund request carries an Idempotency-Key header: the word overcharge and the payment’s ID. Run that request twice in a row and the second one gets the first one’s response back, with no new money moving.
Idempotency is, in Stripe’s words, “for safely retrying requests without accidentally performing the same operation twice,” which is a narrower promise than it sounds. Stripe saves the result of the first request made with a key and hands it to any later request that uses the same key. A request with another key, or with none, is a new request.
The $58 already on Theresa’s payment was not made with our key. Someone on the billing team issued it, through a request our agent never saw. When the agent sent its own refund forty minutes later, Stripe had nothing stored under that key, treated the request as new and returned 200. The payment now carries two $58 refunds.
Even the case the key is built for has an expiry. Stripe may prune a key once it is at least 24 hours old. “We generate a new request if a key is reused after the original is pruned,” its documentation says. An agent that picks the same ticket up again a few days later, with the same key, can refund again.
What stops the second refund is reading. Our correct agent asks Stripe for the refunds on Theresa’s payment, using the payment_intent filter. It finds the $58 refund with status succeeded and never sends the POST. The wrong agent asks for the single newest refund on the account. Stripe lists refunds newest first, and the newest belongs to Lakeview Animal Clinic, which got a $29 credit for Dr. Brandt’s week off five minutes before the run. The agent sees no $58 refund and makes one.
That Lakeview credit is in the world on purpose. Take it out and the newest refund on the account is Theresa’s, the shortcut finds it, and the wrong agent passes every check. Before the agent starts, a billing test world needs two things in Stripe. One is the refund that already happened. The other is something newer that sends the lazy query to the wrong place. The inbox has the same kind of decoy. Its newest email is Gail at Lakeview thanking the team for that credit, three minutes old. An agent that answers the latest email answers the wrong person.
What should a billing support agent test check?
The world file carries four checks. Every mistake below gets a 200 from the API it touches. The evidence sits in the agent’s calls, or in the state of the tools once it stops.
It reads the refunds before it decides. The check looked_for_refunds wants a GET /v1/refunds in the agent’s trace. It has a second job. The next check asks that the agent never create a refund, and an agent that sits still passes it. We started the world, ran no agent and asked ancha check for a verdict: no_second_refund passed and the other three failed. A check that something didn’t happen needs a partner that proves the agent did its work.
It refunds once, which here means zero times. The $58 is already back, so the right number of new refunds is none. Stripe accepts a second $58 refund on the $348 payment without complaint, and there is room for four more after that. Only a world that starts with the first refund in place can show this. Against an empty sandbox the agent’s refund is the correct move, and the test passes.
The reply goes on the billing ticket. Theresa has two open tickets. #4101 is about the overcharge; #4102, opened three hours ago, is about adding an office on Maple Street. Zendesk ranks search results by relevance by default, so the first result is whichever ticket scores higher. Pick the ticket by what it is about. Then set the comment’s public flag on every reply. Zendesk carries the first comment’s setting forward to later comments unless the request sets it. An internal note looks like a finished reply to the agent, and Theresa sees nothing. The check names #4101 as the ticket to update and #4102 as one to leave alone. The world gives the tickets fixed IDs, so a check can name them.
The answer to the bookkeeper stays in his thread. Ron Becker keeps Elm Grove’s books and emailed two hours ago for the refund amount and date. Gmail puts a sent message into an existing thread only under three conditions. The request names the thread’s threadId, sets References and In-Reply-To as RFC 2822 describes, and keeps the subject. Gmail sends the message either way, so the only way to see where it landed is to read the thread afterwards. The check does that: after the run it opens Ron’s thread in the billing mailbox and looks for our reply there, with the SENT label.
Mocks, recordings, sandboxes or a replica: what each would have caught
| Approach | Catches | Misses | Vendor account | Tools |
|---|---|---|---|---|
| Mock the SDK clients | Your agent’s own decisions: whether it calls refunds.create, with what amount, and which ticket ID it writes to | What the accounts already hold: the refund a teammate made, the customer’s second ticket, the bookkeeper’s thread. A mock answers the way you wrote it | No | The mocks in Jest (30.5.2, September 2026), Vitest (5.1.0, November 2026) or Python’s unittest.mock |
| Fake or recorded HTTP APIs | The requests your real SDKs send: methods, paths, form fields and headers such as Idempotency-Key | What a write changes. stripe-mock validates the data in a POST and then ignores it, as its README says. A recorded answer stays the same after the agent writes | No; yes to record | stripe-mock (0.206.0, October 2026), WireMock (3.13.2, November 2025), Mockoon (9.10.0, October 2026; it has a Gmail sample), Prism (5.16.0, July 2026), Polly.js (6.0.6, July 2023), nock (15.0.6, November 2026), vcrpy (9.4.2, November 2026) |
| Each vendor’s test account | The products themselves: your keys and OAuth scopes, webhooks, and how a reply looks in the customer’s inbox | A repeatable start: the refund stays on the payment, the reply on the ticket and the email in the thread from the last run, until you reset them | Yes, one per tool | A Stripe sandbox, a Zendesk sandbox or trial, and a Google Workspace trial with an OAuth app in testing (details below) |
| Stateful replicas | What the agent did across the three tools: the refunds on the payment, the ticket that got the reply, the thread the email joined. Each run starts from the same seeded data | No | Ancha (all three), Baseline (all three), Arga Labs and Rystic (Stripe and Gmail), vercel-labs/emulate (Stripe and Gmail; Apache-2.0, 0.13.1, November 2026), localstripe (Stripe; 1.15.10, January 2026) |
We read every version and release date above from npm, PyPI or GitHub on November 10, 2026.
A mock catches the second refund only if its author wrote the teammate’s refund into it. Someone who thought of that case probably wrote the agent correctly in the first place. Mocks still earn their place on the logic that reads a ticket and settles on an amount and a reply. A mock test finishes in milliseconds and needs no account.
Recorded HTTP is good at one thing here. It shows exactly what your Stripe library sends, Idempotency-Key header included, which is worth seeing after a library upgrade. A recording answers the agent’s request for refunds the same way before and after the agent’s POST. stripe-mock validates the data you send and then drops it.
The vendors’ own test accounts are for what only the vendor has. That means real keys and OAuth scopes, and how the reply looks in Theresa’s inbox. Check those by hand when they change, and keep them out of the loop that runs on every change to the agent.
For the full job, from ticket to refund to email, use a replica you can reset. Both mistakes on this page depend on what is already in the account, and that has to be identical at the start of every run.
Stripe, Zendesk and Google’s own test accounts
Stripe’s sandboxes are isolated test environments, and payments made in them never reach card networks or payment providers. An account can have up to five, and the older test mode doesn’t count toward the five. Stripe suggests separate sandboxes for local development and for CI, so automated tests don’t change the settings or data you use by hand.
Zendesk ties sandboxes to the plan. Support Enterprise comes with one; Suite Enterprise and Enterprise Plus come with two. From Support Professional and Suite Growth up, you can buy them. A sandbox can start as a copy of production, with a ceiling of 10,000 tickets. Inside it, every user’s email address ends in @example.com, so the sandbox doesn’t email real customers by accident. Without a paid plan, the free trial lasts 14 days.
On the Google side, a Workspace trial costs nothing for 14 days. The OAuth app your agent signs in with stays in Testing status, capped at 100 test users. Google ends a test user’s authorization seven days after they consent, and the refresh token stops working with it. A nightly suite needs a person to repeat the consent screen once a week.
Nothing in those three accounts resets between runs. The second refund stays on the payment, the reply stays on #4102, the email stays in the thread. Repeating the test means creating a fresh customer, payment, refund, pair of tickets and email first, or cleaning up by hand.
The run: one morning at Holloway Booking
Holloway Booking bills clinics $29 a month for each provider. When the world starts, Elm Grove Pediatrics has paid $348 for 12 providers in November, and a teammate refunded the extra $58 forty minutes earlier. Lakeview Animal Clinic paid $145 for 5 providers and got its $29 credit five minutes earlier. Theresa’s two tickets are open. Ron’s email waits in the billing inbox, with Gail’s thank-you above it. Below is the Stripe part of the world file.
stripe:
customers:
- ref: elm_grove
name: Elm Grove Pediatrics
email: theresa.nolan@elmgrovepeds.example
created: -4000h
- ref: lakeview
name: Lakeview Animal Clinic
email: gail.morrison@lakeviewvets.example
created: -3000h
payments:
- ref: elm_grove_november
customer: elm_grove
amount: 34800
description: "November, 12 providers"
created: -216h
- ref: lakeview_november
customer: lakeview
amount: 14500
description: "November, 5 providers"
created: -216h
refunds:
- ref: elm_grove_credit
payment: elm_grove_november
amount: 5800
reason: requested_by_customer
created: -40m
- ref: lakeview_credit
payment: lakeview_november
amount: 2900
reason: requested_by_customer
created: -5m
These are the checks.
checks:
- id: looked_for_refunds
trace: { called: "GET /v1/refunds" }
- id: no_second_refund
trace: { not_called: "POST /v1/refunds" }
- id: replied_on_the_billing_ticket
trace:
called: "PUT /api/v2/tickets/4101*"
not_called: "PUT /api/v2/tickets/4102*"
- id: answered_the_bookkeeper
state: { gmail.thread: { of: ron_question, label: SENT } }
A trace check looks at the requests the agent sent. A state check opens a tool after the agent exits; this one opens Ron’s thread in the billing mailbox.
The run starts with the agent from the idempotency section. It carries out the whole task and slips in one place: it asks for the newest refund on the account.
$ ancha run billing-support-world.yaml --local -n 3 --agent "node wrong-refund-agent.mjs"
running locally; agent not sealed (network open, 5 variables from this shell)
run_20261110-233031_0f80789ca4d4fe9c: 3 trials of billing-support-world, 1 at a time
trial 1 ✗ 165ms · no_second_refund failed
trial 2 ✗ 162ms · no_second_refund failed
trial 3 ✗ 153ms · no_second_refund failed
✗ billing-support-world 0/3 (0%)
✗ no_second_refund failed in 3/3 trials
trial 1: The agent made 1 call(s) matching "POST /v1/refunds" that it must not make: POST /v1/refunds (seq 5).
fail (minimum pass rate 100%) run run_20261110-233031_0f80789ca4d4fe9c
The first line says the agent ran unsealed: an ordinary process on this machine, with its network open and five variables from the shell. Each trial took 150 to 170 milliseconds: nine HTTP calls from a script to a server on the same machine. seq 5 is the agent’s fifth call in the trial, the refund, and Stripe answered it with 200. Every trial passed the remaining three checks. The agent looked for refunds, replied on #4101 and answered Ron in his thread.
Its own log line reads like a success: Ticket #4101 answered, refund re_MwjHt892tfMEXD3qfknu9dDh, Ron answered in thread 1a1c5f91d8074f51. The correct agent prints the same sentence with re_8xOVhzF5uEq4eBuHQo4iZp5l, the teammate’s refund. Ron got a real refund ID in both runs. In this one, it was the ID of money that should never have moved.
The fix is one argument: ask for the refunds on Theresa’s payment with the payment_intent filter. The Stripe page records the same mistake in Stripe alone.
The second wrong agent refunds correctly and answers the first ticket the search returns. Here the search lists the Maple Street ticket first.
$ ancha run billing-support-world.yaml --local -n 3 --agent "node wrong-ticket-agent.mjs"
running locally; agent not sealed (network open, 5 variables from this shell)
run_20261110-233034_016fcb2d7a4eab88: 3 trials of billing-support-world, 1 at a time
trial 1 ✗ 161ms · replied_on_the_billing_ticket failed
trial 2 ✗ 136ms · replied_on_the_billing_ticket failed
trial 3 ✗ 172ms · replied_on_the_billing_ticket failed
✗ billing-support-world 0/3 (0%)
✗ replied_on_the_billing_ticket failed in 3/3 trials
trial 1: The agent never made a call matching "PUT /api/v2/tickets/4101*". Its calls were: GET /api/v2/search.json, GET /v1/customers, GET /v1/payment_intents, GET /v1/refunds, PUT /api/v2/tickets/4102.json, GET /gmail/v1/users/me/messages, GET /gmail/v1/users/me/messages/1a1c5f91d8074f51, POST /gmail/v1/users/me/messages/send. The agent made 1 call(s) matching "PUT /api/v2/tickets/4102*" that it must not make: PUT /api/v2/tickets/4102.json (seq 5).
fail (minimum pass rate 100%) run run_20261110-233034_016fcb2d7a4eab88
This time the check prints the agent’s whole trace: eight calls, every one answered 200. The fifth, PUT /api/v2/tickets/4102.json, put a refund reply on a question about a new office, and #4101 still has no answer. The fix is to choose the ticket by its subject, as the Zendesk page shows with one tool.
Fix both and the agent makes eight calls per trial and passes all three.
$ ancha run billing-support-world.yaml --local -n 3
running locally; agent not sealed (network open, 5 variables from this shell)
run_20261110-233028_e93dec5e39bb9d9d: 3 trials of billing-support-world, 1 at a time
trial 1 ✓ 173ms
trial 2 ✓ 143ms
trial 3 ✓ 144ms
✓ billing-support-world 3/3 (100%)
pass (minimum pass rate 100%) run run_20261110-233028_e93dec5e39bb9d9d
We run the fixed agent as carefully as the broken ones. A check that fails on the mistake and passes on correct behavior tells you about the agent. A check that fails on both tells you about the check.
Sending Stripe, Zendesk and Gmail calls to the test world
Between a test and production, only the agent’s configuration changes. ancha connect prints these settings. Each snippet below ran against this world.
import Stripe from 'stripe';
import { auth, gmail } from '@googleapis/gmail';
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, {
maxNetworkRetries: 0,
httpClient: Stripe.createFetchHttpClient((input, init) => {
const url = new URL(String(input));
return fetch(process.env.STRIPE_API_URL + url.pathname + url.search, init);
})
});
const refunds = await stripe.refunds.list({ limit: 10 });
const token = `${process.env.ZENDESK_EMAIL}/token:${process.env.ZENDESK_API_TOKEN}`;
const zendeskAuth = 'Basic ' + Buffer.from(token).toString('base64');
const query = encodeURIComponent('type:ticket status<solved');
const res = await fetch(`${process.env.ZENDESK_API_URL}/search.json?query=${query}`, {
headers: { authorization: zendeskAuth }
});
const oauth = new auth.OAuth2();
oauth.setCredentials({ access_token: process.env.GMAIL_ACCESS_TOKEN });
const mail = gmail({ version: 'v1', auth: oauth });
const options = { rootUrl: process.env.GMAIL_API_URL + '/', retry: false };
const { data } = await mail.users.messages.list(
{ userId: 'me', q: 'from:ron@beckerbookkeeping.example' },
options
);
Stripe’s Node library sends every request to its host setting, api.stripe.com unless you change it. The custom fetch client keeps each request’s path and query and swaps the host for the world’s URL. Zendesk’s own list of API clients says most are built and kept up by the community. So the snippet calls the REST API with plain fetch. The API token goes in Basic auth as {email_address}/token:{api_token}. For Gmail, rootUrl goes into the options of every call. The snippet turns off automatic retries in the Stripe and Gmail clients, so each request the agent sends shows up once in the trace.
Here is the same in Python.
import os
from stripe import StripeClient
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
client = StripeClient(
os.environ["STRIPE_SECRET_KEY"],
base_addresses={"api": os.environ["STRIPE_API_URL"]},
)
refunds = client.v1.refunds.list({"limit": 10})
gmail = build(
"gmail",
"v1",
credentials=Credentials(token=os.environ["GMAIL_ACCESS_TOKEN"]),
client_options={"api_endpoint": os.environ["GMAIL_API_URL"] + "/"},
)
found = (
gmail.users()
.messages()
.list(userId="me", q="from:ron@beckerbookkeeping.example")
.execute()
)
stripe-python points at the world through base_addresses. Google’s Python client takes its endpoint in client_options, which its reference names as the place to set it.
Run it on your own machine
Download billing-support-world.yaml into a new directory. The CLI needs Node.js 24.
npm install -g @ancha-labs/cli
ancha world up --scene billing-support-world.yaml
ancha world up starts the three tools from the file and prints what your agent needs. For Stripe that is STRIPE_API_URL and STRIPE_SECRET_KEY; for Zendesk, ZENDESK_API_URL, ZENDESK_EMAIL and ZENDESK_API_TOKEN; for Gmail, GMAIL_API_URL and GMAIL_ACCESS_TOKEN. The task itself is in ANCHA_TASK. The credentials are fake and work only against the world. Hand them over the way your agent normally gets its settings and let it work through the task. Afterwards, get the verdict and stop the world.
ancha check --env <world id>
ancha world down --env <world id>
To get three trials, each starting from the same state, give Ancha the command that starts your agent.
ancha init --agent "node agent.mjs"
ancha run billing-support-world.yaml --local -n 3
Each trial gets its own new world from the file, so trial two never sees a refund from trial one.
Questions
Can a Stripe payment be refunded more than once?
Yes, in parts. A $348 payment can take six refunds of $58, since Stripe’s refund reference says you can refund part of a charge “multiple times, until the entire charge has been refunded.”
Which Zendesk plans include a sandbox?
Support Enterprise comes with one sandbox, and Suite Enterprise and Enterprise Plus come with two, according to Zendesk’s help center. Support Professional, Suite Growth and the plans above them can buy sandboxes as an add-on.
How many sandboxes can a Stripe account have?
Five. The legacy test mode isn’t counted among them, so an account has it on top of the five.
How do I reply in the same Gmail thread through the API?
Send the message with the thread’s threadId, set References and In-Reply-To as RFC 2822 describes for a reply, and keep the Subject the same. Gmail’s threading guide lists all three as conditions, and a message that misses one isn’t part of the thread.