> For AI agents: the complete Sanity documentation index is available at [https://www.sanity.io/docs/llms.txt](https://www.sanity.io/docs/llms.txt).

# Query instance history and audit trails

Query workflow instance history, resolve actor stamps, separate effect history, and record custom audit entries.

The engine records every step an instance takes in the instance’s `history` array. This guide reads that history for the instance you finished in the quick start. First you read it with the CLI, then from a script. Finally, a GROQ query reads history across many instances for reporting.

You need:

- The finished [quick start](https://www.sanity.io/docs/workflows/getting-started), and the instance ID its `start` command printed. That instance is in `approved`. If you lost the ID, run `npx sanity-workflows list --include-completed` to find it.
- For the script steps, `scripts/engine.ts` from [Run the engine from your own code](https://www.sanity.io/docs/workflows/run-the-engine), and a `SANITY_AUTH_TOKEN` that can read the dataset.

## Show the history with the CLI

Add `--include history` to the `show` command from the quick start:

**npm**

```shell
npx sanity-workflows show INSTANCE_ID --include history
```

**pnpm**

```shell
pnpm dlx sanity-workflows show INSTANCE_ID --include history
```

**yarn**

```shell
yarn dlx sanity-workflows show INSTANCE_ID --include history
```

**bun**

```shell
bunx sanity-workflows show INSTANCE_ID --include history
```

After the stage summary, the **History** section lists every entry in the order the engine wrote it. Your instance ID, timestamps, and user ID will differ:

**CLI**

```text
article-review v1  dev.wf-instance.a1b2c3d4e5f6
  Stage:     approved
  Started:   2026-10-23 14:02:11
  Completed: 2026-10-23 14:05:37
  Partition: dev

Stage history:
  • drafting (exited 2026-10-23 14:03:48)
      ✔ write  [done]
  • review (exited 2026-10-23 14:05:37)
      ✔ sign-off  [done]
  • approved (current)

History:
  [2026-10-23 14:02:11] Stage entered: drafting (by pAbC123xyz)
  [2026-10-23 14:03:48] Action fired: write → submit (by pAbC123xyz)
  [2026-10-23 14:03:48] Activity status changed: write active → done (by pAbC123xyz)
  [2026-10-23 14:03:48] Op applied: status.set write (by pAbC123xyz)
  [2026-10-23 14:03:48] Stage exited: drafting (by pAbC123xyz)
  [2026-10-23 14:03:48] Transition fired: to-review, drafting → review (by pAbC123xyz)
  [2026-10-23 14:03:48] Stage entered: review (by pAbC123xyz)
  [2026-10-23 14:05:37] Action fired: sign-off → approve (by pAbC123xyz)
  [2026-10-23 14:05:37] Activity status changed: sign-off active → done (by pAbC123xyz)
  [2026-10-23 14:05:37] Op applied: status.set sign-off (by pAbC123xyz)
  [2026-10-23 14:05:37] Stage exited: review (by pAbC123xyz)
  [2026-10-23 14:05:37] Transition fired: to-approved, review → approved (by pAbC123xyz)
  [2026-10-23 14:05:37] Stage entered: approved (by pAbC123xyz)
```

Each `fire-action` call wrote six entries. For the first call, the entries record these steps:

1. The `submit` action fired on the `write` activity.
2. `write` changed from `active` to `done`.
3. The `Op applied: status.set` entry recorded the `status: 'done'` that the action declares.
4. The instance left `drafting`.
5. The `to-review` transition fired.
6. The instance entered `review`.

The last three entries come from the transition that fired inside the same call. That chain is the cascade from the quick start.

`(by …)` shows the ID of the user each entry is attributed to. In the quick start, that’s the user you logged in as with the CLI.

## Read the history from a script

In code, `engine.getInstance({instanceId})` returns the complete `WorkflowInstance`. Read `instance.history`, and check each entry’s `_type` to know which fields it has. Create `scripts/show-history.ts`. It imports the engine from `scripts/engine.ts` and prints the stage changes and fired actions:

**scripts/show-history.ts**

```typescript
import type {HistoryEntry} from '@sanity/workflows'

import {engine} from './engine'

async function main() {
  const instanceId = process.argv[2]
  if (!instanceId) {
    throw new Error('Pass the instance id: npx tsx scripts/show-history.ts INSTANCE_ID')
  }

  const instance = await engine.getInstance({instanceId})
  const history: HistoryEntry[] = instance.history

  for (const entry of history) {
    switch (entry._type) {
      case 'stageEntered':
        console.log(`${entry.at}  entered ${entry.stage}`)
        break
      case 'actionFired':
        console.log(`${entry.at}  fired ${entry.activity}/${entry.action} as ${entry.actor?.id}`)
        break
      case 'transitionFired':
        console.log(`${entry.at}  ${entry.transition}: ${entry.fromStage} → ${entry.toStage}`)
        break
    }
  }
}

main().catch((error) => {
  console.error(error)
  process.exit(1)
})
```

Inside each `case`, TypeScript knows which fields the entry has. For example, `activity` and `action` exist only on `actionFired`, and `fromStage` and `toStage` only on `transitionFired`. Run the script with the quick start’s instance ID, and replace `YOUR_TOKEN` with a token that can read the dataset:

**CLI**

```sh
SANITY_AUTH_TOKEN=YOUR_TOKEN npx tsx scripts/show-history.ts INSTANCE_ID
```

The script prints the same events as the CLI’s History section, with full ISO timestamps:

**CLI**

```text
2026-10-23T14:02:11.402Z  entered drafting
2026-10-23T14:03:48.117Z  fired write/submit as pAbC123xyz
2026-10-23T14:03:48.126Z  to-review: drafting → review
2026-10-23T14:03:48.126Z  entered review
2026-10-23T14:05:37.865Z  fired sign-off/approve as pAbC123xyz
2026-10-23T14:05:37.871Z  to-approved: review → approved
2026-10-23T14:05:37.871Z  entered approved
```

The actor in each entry is the user the engine acted for when it wrote the entry. It doesn’t depend on the token that reads the history, so running the script with a different token prints the same IDs.

Usually the actor is the owner of the token that made the change. If a service passes `access` to `createEngine`, the actor is the person that service named.

## Query history across instances

To report across instances, query the workflow resource with the `client` that `scripts/engine.ts` exports. Use this path for bulk reads. For one known instance, `getInstance` is the safer choice.

The query keeps to the engine’s partition. Instance documents store the partition in a field named `tag`, so the query filters on `tag == $tag` and passes the quick start’s partition, `'dev'`, as `$tag`. Create `scripts/list-histories.ts`:

**scripts/list-histories.ts**

```typescript
import {client} from './engine'

async function main() {
  // @sanity/client reserves `tag` as a request option, so type the GROQ params as a record.
  const params: Record<string, string> = {tag: 'dev'}

  const rows = await client.fetch(
    `*[_type == "sanity.workflow.instance" && tag == $tag]{
      _id,
      definition,
      currentStage,
      history
    }`,
    params,
  )
  for (const row of rows) {
    console.log(`${row._id}  ${row.definition}  ${row.currentStage}  ${row.history.length} entries`)
  }
}

main().catch((error) => {
  console.error(error)
  process.exit(1)
})
```

Run it. Each row is one instance in the `dev` partition. The quick start’s instance shows 13 entries, the same number as the CLI’s **History** section. If you ran the script from the engine guide, that instance is listed too:

**CLI**

```sh
SANITY_AUTH_TOKEN=YOUR_TOKEN npx tsx scripts/list-histories.ts
```

**CLI**

```text
dev.wf-instance.a1b2c3d4e5f6  article-review  approved  13 entries
```

## Inspect recorded history events

Every entry has `_key`, `_type`, and `at`. Entries the engine writes can also have `executionContext`, which records where the engine ran. Each event type adds its own fields. [HistoryEntry types](https://www.sanity.io/docs/workflows/engine) in the engine reference lists them.

To watch new entries as a running instance adds them, run `npx sanity-workflows tail INSTANCE_ID`. The quick start’s instance is finished, so start a new instance to try it.

To learn what the actor and execution context record, and why they don’t prove who acted, see [Actor identity and tokens](https://www.sanity.io/docs/workflows/actors-and-enforcement).

Effect history, activity feeds, retention, and custom audit rows are covered in [Instance history and audit trail contracts](https://www.sanity.io/docs/workflows/engine).

## Next steps

- [Engine, effects, and insights reference](https://www.sanity.io/docs/workflows/engine): history read paths, entry types, and retention.
- [Definitions, stages, and instances](https://www.sanity.io/docs/workflows/definitions-and-instances): the durable instance document.
- [Run an external step with an effect](https://www.sanity.io/docs/workflows/effects-and-runtimes): effect delivery and the separate result ledger.
- [Define activities, actions, and operations](https://www.sanity.io/docs/workflows/activities-and-actions): the state changes recorded in history.
- [Actors and enforcement](https://www.sanity.io/docs/workflows/actors-and-enforcement): who an entry is attributed to and what that proves.

> [!NOTE]
> Visiting agent?
> Workflows has MCP tools that let an agent inspect, operate, author, validate, and deploy workflows. An agent host embeds these tools. Ask the person you work for whether your host [includes the Workflows tools](https://www.sanity.io/docs/workflows/mcp).



