> ## Documentation Index
> Fetch the complete documentation index at: https://docs.60db.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Fix Agent from a Simulation

> Improved instructions written from what failed in a finished simulation run

> [!NOTE]
>
> AI Simulation needs an active **Pro** or **Business** plan. On another plan, every simulation endpoint returns `403 POLICY_DENY`.

Write improved instructions for the agent from the failures of a finished run. This makes one paid model request and saves nothing. Show the result as a preview, then save it with **apply** below.

Only failures that new instructions can fix are used: failed checks (a phrase missing or said, a tool used or not used, a detail, the caller's goal) and calls that did not do what the story expected. Speed and service problems are left out.

## Request

### Headers

**Authorization** `string` *required*

Bearer token with your API key (scope `agents`)

### Path

**runId** `string` *required*

The `run_id` of a finished run from [Start a simulation](https://docs.60db.ai/api-reference/agents/start-simulation.md)

## Response

**200 OK**.

```json theme={null}
{
  "agent": { "id": "8f8d15b7-ea7e-42ef-b3a6-dd78d9dde271", "name": "Loan bot" },
  "base_hash": "3b1f…",
  "before": "You are Loan bot. Greet the caller and confirm their PAN.",
  "after": "You are Loan bot. Greet the caller, read the PAN back digit by digit, and confirm it.",
  "changes": [{ "summary": "Read the PAN back digit by digit.", "problem_ids": ["contains"] }],
  "problems": [{ "category": "contains", "count": 3, "reason": "The agent did not say \"PAN verified\" when the story needed it." }],
  "settings_problems": [{ "category": "tool_called", "tool": "webhook_shop__check_product", "count": 1, "reason": "The stories expect the tool webhook_shop__check_product, which is attached to this agent, but its instructions say not to use tools." }]
}
```

The suggestion is built from what the agent actually said in the failed calls, so a check the agent already met in other words does not produce a new rule. New rules go in an `ADDITIONAL RULES` section at the end of the instructions; a rule the instructions already have is not added again.

**base\_hash** `string`

SHA-256 of `before`. Send it with apply, so a changed agent is never overwritten.

**changes** `array`

What changed, in plain words

**problems** `array`

The failures the new instructions answer

**settings\_problems** `array`

Failures new instructions cannot fix: a tool the stories expect that the agent lacks, or one its instructions rule out. Present only when there are any

## Apply and undo

`POST /agents/simulations/{runId}/fix/apply` saves `after` to the agent. Body: `{ "base_hash": "<from the suggestion>", "script": "<after>" }`. The answer has the hash of the saved text:

```json theme={null}
{ "agent": { "id": "8f8d15b7-…", "name": "Loan bot" }, "base_hash": "9c4a…" }
```

The new instructions pass the same checks as a fix from Call analysis: not empty, not the same text, within the length a call can use, and no new placeholder, link, e-mail address or phone number.

`POST /agents/simulations/{runId}/fix/undo` puts the earlier text back. Body: `{ "base_hash": "<from apply>", "script": "<before>" }`.

Both need the `agent:write` permission.

## Errors

| Status | Code | Meaning |
| - | - | - |
| 400 | `NOTHING_TO_FIX` | The run's failures are only speed or service problems |
| 400 | `SETTINGS_PROBLEM` | The only failures left are settings problems (see `settings_problems`) |
| 409 | `FIX_ALREADY_APPLIED` | A fix from this run is already applied. Re-run the stories (`agent: "current"`) to check it, or undo it first |
| 400 | `VALIDATION_ERROR` | Apply or undo: `base_hash` or `script` is missing or not valid |
| 403 | `POLICY_DENY` | Your role cannot do this, the workspace is not on an active Pro or Business plan, or the API key lacks the `agents` scope |
| 404 | `RUN_NOT_FOUND` | No run with this ID in your workspace |
| 404 | `AGENT_NOT_FOUND` | The agent the run tested no longer exists |
| 409 | `RUN_ACTIVE` | The run is still running or paused |
| 409 | `AGENT_CHANGED` | Apply or undo: the agent changed since the preview. Ask for a new suggestion. |
| 422 | `FIX_INVALID` | Apply: the new instructions fail a check above |
| 429 | | More than 3 suggestions in a minute |
| 503 | `AGENT_FIX_UNAVAILABLE` | The model is not available. Try again. |

## Example

```bash cURL theme={null}
curl -X POST https://api.60db.ai/agents/simulations/RUN_ID/fix \
  -H "Authorization: Bearer your-api-key"
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.