---
title: Training environments for agents that use APIs | Twinbay
description: Give agents isolated, stateful versions of the APIs they use. Set the starting state, run through the real SDK, and evaluate every outcome.
doc_version: ga
last_updated: 2026-10-16
---

<!-- markdownlint-disable-next-line MD025 -->
# Training environments for agents that use real-world APIs

Twinbay gives agents isolated, stateful versions of the APIs they depend on.
Set the starting state, let the agent act through the real SDK, and evaluate
every outcome without touching production.

## From scenario to evaluated run

1. Define a scenario with the providers and starting data the agent should
   encounter.
2. Start an isolated environment and let the agent act through its existing
   SDKs and tools.
3. Grade the captured traffic against outcomes that must happen or must never
   happen.

## Agent environments

Each environment provides:

- A private hostname and state for each attempt.
- A working account or reusable seed that defines the starting state.
- Credentials in the provider's format.
- Records that keep every write the agent makes.
- An action history containing method, body, status, and latency.

Because an environment is stored state rather than a dedicated container, it is
available immediately, has no cold start, and costs nothing while idle.

## Provider behavior

Twins are hand-written for the providers agents rely on. They model
provider-specific state machines, pagination, idempotency, authentication,
errors, and other behavior. Each one is checked in CI with the provider's
official SDK, and unmodelled routes fail explicitly.

## Available twins

- **Resend:** Email delivery lifecycles, scheduled sends, domains, API-key
  permissions, contacts, segments, broadcasts, pagination, and idempotency.

## Training loop

1. Choose a saved scenario or pick the provider twins for this run.
2. Load the starting state from reusable seeds.
3. Point the agent's existing SDKs at the private hostnames.
4. Run the agent and capture every request and resulting state change.
5. Evaluate the run, then start another isolated attempt from the same scenario.

## Agent interface

Create and inspect environments through the Twinbay web app, the [Twinbay
CLI](https://github.com/panoratech/twinbay-cli), or the MCP server at
`https://mcp.twinbay.ai/mcp`. The MCP server exposes the same twin, environment,
state and request-log operations as the API, as tools named after them
(`list_twins`, `create_environment`, `collect_environment_twin_credential`,
`get_request_log`); a client signs in through AuthKit rather than carrying a
key.

```bash
twinbay environments create --name=support-run --twins='[{"twin":"resend"}]'
twinbay environment-twins collect-credential --environment-id=$ENVIRONMENT --environment-twin-id=$TWIN
twinbay environment-logs list --environment-id=$ENVIRONMENT
```

A provider SDK only needs its base URL and key changed:

```python
import resend

resend.api_key = "re_twinbay_9f2a…"
resend.api_url = "https://resend-a1c93f.twinbay.run"

email = resend.Emails.send({
    "from": "onboarding@resend.dev",
    "to": "delivered@resend.dev",
    "subject": "Hello from the environment",
    "html": "<strong>It works.</strong>",
})
```

## What a twin guarantees

A twin will not:

- Invent a response for a route it does not model.
- Return a generic 404 for an endpoint that exists but is unmodelled.
- Claim behavioral fidelity without passing the provider SDK in CI.
- Share state with another environment or another team.

## Glossary

- **API digital twin:** A stateful environment that reproduces an API's
  observable payloads and behavior.
- **Environment:** One isolated instance of a twin with its own hostname,
  credentials, records, and request log.
- **Twin:** A hand-built provider clone validated against the official SDK.
- **Directive:** A named request that puts a twin in a state the provider API
  cannot create directly, such as a spent rate limit or a failed payment.
- **Provider-shaped credential:** An environment credential formatted like the
  provider key it replaces.
- **Not modelled:** An explicit response indicating that a known provider
  operation is outside a twin's fidelity.

## Access

Twinbay is live and free for academic researchers. [Create an environment in the
Twinbay console](https://console.twinbay.ai) with an email address. No invite
code is required.

## Sitemap

- [Homepage](/)
- [Agent guide](/AGENTS.md)
- [LLM index](/llms.txt)
- [Markdown sitemap](/sitemap.md)
- [XML sitemap](/sitemap.xml)
