# run()

Canonical URL: https://developers.iagenify.com/sdk/ref/local-agent/run/
Language: en

`agent.run({ message | inputs, history?, onEvent?, edit? }) → Promise<Result>`

One exchange. It loops until the model stops asking for tools, streaming everything through onEvent, and returns what it ended with.

Status: the loop · onEvent cannot break it · one at a time

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `message` | string |  | The text. The short form of `inputs`. |
| `inputs` | array |  | `[{ type: "text", content }, { type: "image", content: base64, mime }]`. |
| `history` | array |  | `[{ role, content }]` — a previous conversation, for an agent without a session. |
| `onEvent` | function |  | Receives every [event](/sdk/agent/events). Never awaited; anything it throws is swallowed. |
| `edit` | integer |  | Go back to that message first, then run with the new text. |

One of `message` and `inputs` is required — neither, and it throws `inputs_required`.

## Sending an image {#images}

```javascript
await agent.run({
  inputs: [
    { type: "text", content: "What is wrong with this layout?" },
    { type: "image", content: base64, mime: "image/png" },
  ],
});
```

## Returns {#returns}

```javascript
{
  text: "I read the four notes. Two are unfinished: …",
  todos: [{ content: "Check the figures", status: "completed" }],
  usage: { credits: 31, turns: 6, toolCalls: 9 },
  stopReason: "end_turn",      // "stopped" | "max_steps" | "budget"
  model: "gemini-3.5-flash"
}
```

> **Read stopReason before using text**
>
> Only end_turn means the model finished saying what it had to say. On max_steps or budget, text is whatever it last wrote — possibly mid-plan.

## history, for an agent without a session {#history}

It seeds the conversation when the agent holds none. With `memory: true` the session's own history wins, and this is ignored — which is the right shape when the conversation belongs to your database rather than to ours.

## edit: changing an earlier message {#edit}

```javascript
await agent.run({
  edit: 2,
  message: "Actually, rename them to kebab-case.",
});
```

It rewinds to just before message 2 — files included — emits `rewound`, then runs with the new text. It needs a session and checkpoints, and it throws before starting if either is missing.

## What makes it reject {#errors}

A turn that failed on every model after every retry, a context that will not fit, an unauthorised session, or a credit refusal. Everything else — a tool that threw, a tool that was refused — is handled inside the loop and never surfaces here.

```javascript
import { NotEnoughCredits, IaGenifyError } from "iagenify";

try {
  await agent.run({ message });
} catch (err) {
  if (err instanceof NotEnoughCredits) { /* never retried, on any model */ }
  else if (err instanceof IaGenifyError && err.code === "context_too_large") await agent.compact();
  else throw err;
}
```

- [The loop](/sdk/agent/loop): What a step is.
- [Events](/sdk/agent/events): Everything onEvent receives.
- [stop()](/sdk/ref/local-agent/stop): Ending it early.

```python
agent.run(inputs=[
    {"type": "text", "content": "What is wrong with this layout?"},
    {"type": "image", "content": base64_png, "mime": "image/png"},
])
```

```python
{
    "text": "I read the four notes. Two are unfinished: …",
    "todos": [{"content": "Check the figures", "status": "completed"}],
    "usage": {"credits": 31, "turns": 6, "toolCalls": 9},
    "stopReason": "end_turn",      # "stopped" | "max_steps" | "budget"
    "model": "gemini-3.5-flash",
    "context": {},                 # the context meter, as in agent.context
}
```

```python
agent.run(
    edit=2,
    message="Actually, rename them to kebab-case.",
)
```

```python
from iagenify import IaGenifyError, NotEnoughCredits

try:
    agent.run(message=message)
except NotEnoughCredits:
    pass  # never retried, on any model
except IaGenifyError as error:
    if error.code != "context_too_large":
        raise
    agent.compact()
```
