# run()

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

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

Un intercambio. Il loops fino a che il modele para de pedir utensili, streaming di tutto onEvent, e restitue con quello con cui terminò.

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

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `message` | string |  | Il testo. La forma corta di `inputs`. |
| `inputs` | array |  | `[{ type: "text", content }, { type: "image", content: base64, mime }]`. |
| `history` | array |  | `[{ role, content }]` — una conversazione precedente, per un agente senza sessione. |
| `onEvent` | function |  | Receve ogni [event](/pt/sdk/agent/events). Mai atteso; tutto ciò che lancia è inghiottito. |
| `edit` | integer |  | Torna a quel messaggio prima, poi corre con il nuovo text. |

Uno di `message` e `inputs` né necessari — né, e lança `inputs_required`.

## Enviando una imagem {#images}

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

## Retorno {#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"
}
```

> **Ler stopReason prima di usar text**
>
> Solo end_turn significa il modello che ha finit di dire quello che ha a dir. In max_steps o budget, il testo è quello che ha scritto ultimamente — possibly mid-plan.

## historis, per un agente senza sessione {#history}

Con `memory: true` la propria storia della sessione vince, e questo è ignorat — che è la forma giusta quando la conversazione appartiene a sua base di dati e non a la nostra.

## edit: cambiando un messaggio anterior {#edit}

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

Rembobina a poco prima del messaggio 2 — files inclusi — emite `rewound`, poi run con il nuovo testo. Necessita una sessione e checkpoints, e lança prima di iniziare se uno dei due è mancant.

## Ce lo fa respingere {#errors}

Un virat che non ha scaduto su ogni modela dopo ogni tentazione, un contexto che non vat, una sessione non autorizzata, o un renego de credit.Tudo il resto — un utrel che lança, un utrel che è stato rifiutat — è maneggiat dentro del loop e mai supere qui.

```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;
}
```

- [O ciclo](/pt/sdk/agent/loop): Que pas è.
- [Eventos](/pt/sdk/agent/events): Tutto oevenimente riceve.
- [stop()](/pt/sdk/ref/local-agent/stop): Finendo il prematuramente.

```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()
```
