Lesson 19.3 · 3 of 4 · 4 min read · Reviewed 24 Nov 2026
Simulated users and environments
By the end, you can
- Use simulated users and environments to test agents.
- Keep simulations realistic and safe.
Simulated users
A simulated user is a model prompted with a persona and a goal:
You are a customer. Persona: in a hurry, writes short messages, gives
the order number only when asked. Goal: change delivery to Friday.
Stop when the agent confirms the change or after 8 turns.
Vary personas: patient, angry, confused, non-native speaker, someone who changes their mind, someone trying to break the rules.
Simulated environments
The agent’s tools point at a sandboxSandbox: An isolated environment that limits what code or an agent can reach, such as files, network or credentials.:
| Real | Sandbox |
|---|---|
| Orders database | Seeded test database reset per run |
| Payment API | Fake that records calls |
| Captured, never sent | |
| Web | Fixed snapshots of pages |
At the end, inspect the sandbox state for the outcome evalEval: A repeatable test that measures how well an AI system performs on a set of cases with defined success criteria. (19.1).
Keep it honest
- Compare simulated conversations with real ones. Simulated users are often too polite and too clear.
- Seed some simulations from real (redactedRedaction: Removing or masking sensitive data, such as PII or secrets, before it is stored, logged or sent.) opening messages.
- Score the agentAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done., not the simulator. If the simulated user went off script, mark that run invalid.
This site’s Playground works the same way: tools and models are simulated in your browser, so nothing real is touched.
Key points
- Multi-turn agents need a counterpart: a simulated user, often a model given a persona and a goal.
- Test in a sandbox environment with fake data and fake tools, never production systems.
- Check simulations against real conversations so the simulated users behave like real ones.
Drive an agent with simulated users
Run a toy delivery agent against simulated users with different personas, in a sandbox, and score the outcome.
You will have: A simulation harness that runs three personas with a turn cap and prints the outcome per persona.
Steps
- Read the toy agent: it asks for an order number, then changes the delivery day.
- Fill in the simulated user so each persona gives the order number only when its rule allows.
- Run each persona for at most 8 turns and inspect the sandbox for the outcome.
- Mark a run invalid if the simulated user goes off script, rather than blaming the agent.
Done when
- The cooperative and forgetful personas both end with delivery set to Friday.
- The off-script persona is reported as invalid, not as an agent failure.
- No run exceeds 8 turns.
Open in the Playground No API key needed.
Starter code (Python)
import re
def agent(history, sandbox):
"""A toy agent: needs an order number, then sets the delivery day."""
text = " ".join(m for role, m in history if role == "user")
order = re.search(r"\b(\d{4})\b", text)
if "friday" not in text.lower():
return "What would you like to change?"
if not order:
return "Could you tell me your order number?"
sandbox["orders"][order.group(1)] = "Friday"
return f"Done: order {order.group(1)} now arrives on Friday."
PERSONAS = {
"cooperative": {"opening": "Please move order 5521 to Friday.", "order": "5521"},
"forgetful": {"opening": "Can my parcel come on Friday instead?", "order": "5522"},
"off_script": {"opening": "Do you sell garden furniture?", "order": "5523"},
}
def simulated_user(persona, agent_msg, turn):
# TODO: reply to the agent. Give the order number only when asked for it.
# TODO: the off_script persona keeps asking about other products; return None to stop.
return None
for name, p in PERSONAS.items():
sandbox = {"orders": {}}
history = [("user", p["opening"])]
# TODO: loop up to 8 turns: agent replies, simulated user replies, stop on "Done" or None
print(name, "->", sandbox["orders"])
Show a solution
import re
def agent(history, sandbox):
"""A toy agent: needs an order number, then sets the delivery day."""
text = " ".join(m for role, m in history if role == "user")
order = re.search(r"\b(\d{4})\b", text)
if "friday" not in text.lower():
return "What would you like to change?"
if not order:
return "Could you tell me your order number?"
sandbox["orders"][order.group(1)] = "Friday"
return f"Done: order {order.group(1)} now arrives on Friday."
PERSONAS = {
"cooperative": {"opening": "Please move order 5521 to Friday.", "order": "5521"},
"forgetful": {"opening": "Can my parcel come on Friday instead?", "order": "5522"},
"off_script": {"opening": "Do you sell garden furniture?", "order": "5523"},
}
def simulated_user(name, persona, agent_msg, turn):
if name == "off_script":
return "What about patio heaters?" if turn < 3 else None
if "order number" in agent_msg:
return f"Oh, it's {persona['order']}."
return "Friday, please."
MAX_TURNS = 8
for name, p in PERSONAS.items():
sandbox = {"orders": {}}
history = [("user", p["opening"])]
status, turns = "unfinished", 0
for turn in range(MAX_TURNS):
turns = turn + 1
reply = agent(history, sandbox)
history.append(("assistant", reply))
if reply.startswith("Done"):
status = "resolved"
break
user = simulated_user(name, p, reply, turn)
if user is None:
status = "invalid (user went off script)"
break
history.append(("user", user))
outcome = sandbox["orders"].get(p["order"]) == "Friday"
print(f"{name}: {status} in {turns} turn{'s' if turns != 1 else ''}, outcome {'PASS' if outcome else 'n/a' if status.startswith('invalid') else 'FAIL'}")
Do it Run an agent against simulated tools in the Playground. No API key needed.