ai-development

Stateful API simulation for AI agents: what it is and why multi-step tests need it

Eran Levy Growth @ WireMock
December 1, 2026
Stateful API simulation for AI agents: what it is and why multi-step tests need it

This post explains what stateful API simulation is, why an AI agent needs it when you test the agent against simulated APIs, and how to set it up by prompting a coding agent.

What stateful API simulation is

A simulated API stands in for a real API during tests: it accepts the same requests and answers in the same format. In a static mock, each request gets a response written in advance, whatever happened before it. A stateful simulated API stores what requests create or change, and uses that in later responses, the way the real API would.

Take a billing agent that, once a week, creates and sends a PayPal invoice for every finished job. It creates a draft invoice through PayPal’s Invoicing API, then searches for drafts to send. A static simulated API answers the search with the same fixed list as before, which doesn’t include the new invoice. A stateful one includes it, and after the agent sends the invoice, it shows the invoice as sent.

To learn more about stateful simulation, read:

Why an agent needs state more than ordinary test code

In a test written in code, you know which calls the code will make, so you choose a fixed response for each one. If a response doesn’t fit what the code did, the test fails, you see it and you fix the test. An agent decides its next call from the answers it gets, and it can make calls you didn’t plan for. When a fixed response doesn’t match what the agent just did, nothing fails: the agent takes the answer as true and carries on, e.g. it creates the same record again or reports a mismatch that isn’t there.

Four common ways this goes wrong:

  • The agent searches before it creates. To avoid creating a record twice, an agent usually searches for it first, e.g. for an invoice with this job’s number. A static search result is either always empty or always a match. If it’s always empty, the agent creates every time and its duplicate check is never tested. If it’s always a match, the create path is never tested.
  • The agent reads back what it wrote. Agents often confirm a change by reading the record afterwards. A static response returns a fixed example record that doesn’t match what the agent wrote, so the agent reports a mismatch or tries to correct one. When we built an accounts-payable agent for an earlier post in this series, the shared public sandbox returned the same fixed wire transfer on every lookup, so the agent warned about an amount mismatch on every invoice it paid.
  • The agent acts on a status. What an agent does next often follows from a status it reads. In PayPal, when a job is called off, a draft invoice for it can be deleted, but a sent one has to be cancelled instead. A static response always returns the same status, so every run takes the same branch and the others are never tested.
  • The agent picks up where it stopped. If a run stops halfway, the next run usually works out what’s already done by reading the records, unless the agent keeps its own progress file. A static API never shows the earlier run’s work, so the second run starts from nothing each time and the way the agent finishes an interrupted job is never tested.

A stateful simulated API answers the agent’s calls the way the real API would for a whole run. So one test can cover the whole workflow, reach a different branch by loading different records before the run, and check the records the agent left instead of trusting its report. Resetting the state before each run means every run starts from the same data, so a difference between two runs comes from the agent, not from leftover records.

What you can test with stateful simulation for agents, in four panels: test the whole workflow in one run (the J-102 invoice goes from draft to found to sent); load different records, reach each branch (no invoice, J-102 sent and a J-103 draft lead to create and send, leave it alone, send the draft); check the records it left, not its report (the agent says no duplicates, the count shows 4 invoices for 3 jobs); reset, so every run starts the same (runs 1 to 3 all start from the same J-103 draft)

Stateful mocking alternatives for agentic development

To test an agent that works in several steps, the simulated APIs it calls have to keep track of what it does: a record the agent creates in one call has to be there when it looks for it in the next. There are three common ways to set that up: a mocking tool, a mock server your coding agent writes for you when you ask (often called vibe-mocking), or a hosted simulated API such as WireMock Cloud.

Mocking toolsVibe-mockingWireMock Cloud
What it isA mock server or library where you write each responseA mock server your coding agent writes when you ask for oneA hosted simulated API, built from the API’s spec or from recorded traffic
State between callsUsually none. Some tools let you set a sequence of responses by handWhatever the coding agent writes, often an in-memory list that empties when the server restartsRequests create, change and read stored values, the way the real API’s records work
How close it is to the real APIAs close as the responses you writeAs close as the coding agent’s idea of the API, which tends to leave out error formats, validation and edge casesStarts from the real API’s spec or real traffic, and you keep adjusting it
Who uses itOften one developer, on their machineThe developer and the agent that wrote itThe whole team and CI, at the same URL

Vibe-mocking is the quickest to start, and that’s why it’s common. The trouble comes when the agent you’re testing depends on how the real API behaves. A mock that the coding agent imagined can pass a test that the real API would fail, and you find out in production. With WireMock Cloud you can record real traffic, make those endpoints stateful, and then change the simulation as you learn more about the API, so the agent is tested against the real formats and behaviour.

How to set up a stateful simulated API with a coding agent

With the WireMock MCP server connected, the coding agent does the setup: one prompt makes a simulated API’s endpoints stateful. If WireMock’s agent skills are installed as well, the coding agent follows their convert-to-stateful skill, which also adds a not-found response for records that don’t exist and checks the result by creating a record and reading it back.

The video below shows us giving that prompt for the billing agent, starting from WireMock’s PayPal template, then what the stateful API looks like in WireMock Cloud.

Prompt for your coding agent

Using the WireMock Cloud MCP server, create a simulated PayPal API from the PayPal template and make its invoicing endpoints stateful: create, show, list, search, send, delete and cancel invoice. A new invoice starts as DRAFT and should come back from show, list and search. Search should filter on invoice_number and status. Sending a draft makes it SENT, and sending it again changes nothing. Delete works only on DRAFT or SCHEDULED invoices; for a SENT invoice, return PayPal’s 422 error, because a sent invoice has to be cancelled instead. Return a 404 for an invoice that doesn’t exist. Then write a seed script for three starting states, each of which resets the state first: no invoices; an invoice already sent for job J-102; and a draft for job J-103 that was never sent.

In our run:

  • The coding agent found that the PayPal template had no invoice endpoints apart from a not-found response, so it wrote the invoice stubs itself, ten in all, and tested each rule with curl requests. That took about seven minutes. Its report also listed what it hadn’t modelled: paging, invoice totals, a check for duplicate invoice numbers, and refusing to cancel a draft. Those are worth adding if your agent relies on them.
  • For the starting states, it added a seed endpoint to the simulated API that loads an invoice with a fixed ID, and wrote a script that resets the state through WireMock Cloud’s API (which needs an API token) and then calls that endpoint.
  • A test run then has three parts: load a starting state, run the agent, and read the invoices back through the simulated API to compare them with what that state should end with. For the third state, that’s one invoice per finished job, all sent, and no second draft for J-103.

How to steer your agent in the right direction

Before writing a prompt like this one, it’s worth thinking through three things.

Which endpoints to make stateful. You only need state for endpoints the workflow writes to and later reads, searches or acts on. A workflow that only reads (looking up an order, summarising tickets) gets the same answers from static responses. Stateful endpoints have more rules that have to match the real API: what search returns, which status changes are allowed, which errors come back. That’s more for the agent to get wrong and more for you to check, and they need a reset before each run, while static responses come back the same every time.

How many starting states to test. One for each branch the agent can take. For the billing agent that’s three: no invoice yet, an invoice already sent, and a draft left by a run that stopped before sending. Two or three usually cover the branches that create or change records. Because the agent can behave differently each time, each state is worth running more than once.

Which context to add. The coding agent can read the template’s endpoints for itself, but it can’t know which rules your workflow depends on. So the prompt names the records the workflow uses, the rules the real API applies to them, and the starting data for each state. We took the rules in our prompt from PayPal’s Invoicing API reference. Where the docs leave a gap, the choice is yours: PayPal’s reference doesn’t list an error for deleting a sent invoice, so the 422 in our prompt is our own choice, the status PayPal’s REST APIs use for a request that fails a business rule.

Summary and where to go next

A static simulated API answers each call on its own, while an agent’s calls build on each other: it searches, creates, reads back and acts on what it finds. A stateful simulated API answers those calls the way the real API would, so one test can run a whole workflow, reach different branches from different starting data, and check the records the agent left. The work for you is deciding which endpoints need state, which rules they follow and which starting states to test; the coding agent builds the rest.

To test what the agent does when those APIs fail, our post on timeouts, rate limits and errors goes through it step by step. A later post in this series will cover running these tests in CI. The PayPal API used here is one of WireMock’s API templates.

Have More Questions?

Explore the docs or get in touch with our team.