# Overview

Canonical URL: https://developers.iagenify.com/sdk/agent/events/
Language: en

One callback receives everything the engine does, as it does it: the text as it streams, each tool before and after, each permission asked and answered, each turn's cost and context.

Status: onEvent · never blocks the loop · children carry parentId

```javascript
await agent.run({
  inputs: "Read the notes and list what is unfinished.",
  onEvent: (event) => {
    switch (event.type) {
      case "text_delta": process.stdout.write(event.delta); break;
      case "tool_start": console.log("→", event.tool, event.args); break;
      case "tool_end":   console.log("←", event.tool, event.ok ? "ok" : "failed", event.summary); break;
      case "turn":       console.log(`${event.totalCredits} credits · ${event.contextTokens} tokens`); break;
      case "done":       console.log(event.stopReason); break;
    }
  },
});
```

## Every event {#all}

| Type | When | Page |
| --- | --- | --- |
| `command` | A `/skill` at the start of the message was expanded | — |
| `mention` | An `@path` or `#storage` was attached | — |
| `checkpoint` | The workspace was photographed before your message | [checkpoint](/sdk/agent/events/checkpoint) |
| `checkpoint_partial` | That snapshot could not take everything | [checkpoint_partial](/sdk/agent/events/checkpoint-partial) |
| `rewound` | A `run({ edit })` went back first | [rewound](/sdk/agent/events/rewound) |
| `compacted` | The older conversation became a summary | — |
| `retry` | A turn failed for a passing reason and is tried again | — |
| `fallback` | A model gave up and the next one is used | — |
| `text_delta` | A piece of the answer, as it streams | [text](/sdk/agent/events/text) |
| `turn` | A model turn finished | [turn](/sdk/agent/events/turn) |
| `text` | The complete text of a turn | [text](/sdk/agent/events/text) |
| `permission` | Something is being asked | [permission](/sdk/agent/events/permission) |
| `permission_result` | You answered | [permission](/sdk/agent/events/permission) |
| `tool_start` | A tool is about to run | [tool_start](/sdk/agent/events/tool-start) |
| `tool_end` | It finished, failed or was refused | [tool_end](/sdk/agent/events/tool-end) |
| `todo` | The plan changed | [todo](/sdk/agent/events/todo) |
| `question` | The agent is asking you something | [question](/sdk/agent/events/question) |
| `question_result` | You answered it | [question](/sdk/agent/events/question) |
| `error` | The run is about to fail | [error](/sdk/agent/events/error) |
| `done` | The run ended | [done](/sdk/agent/events/done) |

## The order in a step {#order}

```bash
checkpoint                 (once, before your message is taken)
  turn                     one per step
    text_delta …           while the answer streams
    text                   the complete text, if there was any
    permission             only if something must be asked
    permission_result
    tool_start
    tool_end
    todo                   if the plan changed
  turn                     next step…
done
```

## Your callback cannot break the run {#safe}

> **Anything it throws is swallowed**
>
> A failing interface must not stop an agent mid-task, so an exception from onEvent is caught and ignored. The consequence is yours to know: an error in your handler leaves no trace. Log inside it if you need one.

It is awaited nowhere either — returning a promise does not make the loop wait. Do slow work outside the callback.

> **A subagent's events arrive here too**
>
> They carry parentId — the identifier of the task call that started them — and agent, its name. Their own id is prefixed with the task's, so two runs never collide. Test parentId before treating an event as the main run's.

## Two ways to get the text {#two-texts}

`text_delta` streams it as it is produced; `text` arrives once with the whole of that turn. Use the first for a live display and the second as the record — and do not append both, or every answer appears twice.

- [turn](/sdk/agent/events/turn): Cost, context and which model served it.
- [tool_start](/sdk/agent/events/tool-start): What is about to happen.
- [The loop](/sdk/agent/loop): Where in a step each one is emitted.

```python
import sys

def on_event(event):
    kind = event["type"]
    if kind == "text":
        sys.stdout.write(event["text"])
    elif kind == "tool_start":
        print("→", event["tool"], event["args"])
    elif kind == "tool_end":
        print("←", event["tool"], "ok" if event["ok"] else "failed", event["summary"])
        print(f"{agent.usage['credits']} credits · {agent.context['tokens']} tokens")
    elif kind == "done":
        print(event["stopReason"])

agent.run(inputs="Read the notes and list what is unfinished.", on_event=on_event)
```
