# Artifacts

> Keep large bytes out of the journal with ctx.artifact, and pass references between steps.

Source: https://cairnflow.dev/docs/develop/artifacts

Step results, state and call inputs live in the journal, and the journal is replayed on every turn.
Large values there make every turn slower and more expensive. **Artifacts** keep the bytes in
object storage and put only a small reference in the journal.

```ts
const ref = await ctx.run("render", async () =>
  ctx.artifact.put(await renderChart(data), { contentType: "image/png" }),
);
// ref = { sha256: "…", size: 48213, contentType: "image/png" }
```

Return the reference from `ctx.run`, not the bytes. The journal then holds a few dozen bytes, and
the step is not re-run on replay.

## API

```ts
interface ArtifactRef {
  readonly sha256: string;      // hex SHA-256 of the content
  readonly size: number;
  readonly contentType: string;
}

ctx.artifact.put(bytes: Uint8Array | string, opts?: { contentType?: string }): Promise<ArtifactRef>;
ctx.artifact.get(ref: ArtifactRef | string): Promise<Uint8Array>;
```

- `put` accepts bytes or a string (encoded as UTF-8). `contentType` defaults to
  `application/octet-stream`.
- The reference is computed locally from the content; cairnflow stores the bytes before it processes
  the invocation's next command.
- Content-addressed: putting the same bytes twice gives the same `sha256`.
- `get` accepts a reference or a bare `sha256` string.

## Limits

- One artifact can be at most 16 MiB minus 1 KiB. The SDK exports this as `ARTIFACT_MAX_BYTES`;
  `put` throws a `RangeError` above it.
- Your environment may also cap the total artifact bytes it keeps; on Cairnflow Cloud, see
  [Beta limits](/docs/cloud/limits).

## Reading an artifact back

`ctx.artifact.get(ref)` first looks in the SDK process's own memory (a 64 MiB cache of recent
artifacts). On a different process — after a restart, or on another replica — it fetches the
artifact from cairnflow, which you must configure once at startup:

```ts

configureArtifacts({
  baseUrl: process.env.CAIRN_URL!,
  headers: { authorization: `Bearer ${process.env.CAIRN_KEY}` },
});
```

Use a key that can invoke handlers. Without this, `get` throws when the artifact is not in memory.

:::caution
`ctx.artifact.get` reads artifacts of the **current** invocation. To hand an artifact to another
invocation, pass the bytes through a new `put` there, or read it over HTTP with the ID of the
invocation that created it.
:::

## Reading artifacts over HTTP

With a key that can invoke handlers:

```sh
# List the artifacts of an invocation.
curl "$CAIRN_URL/invocations/$ID/artifacts" -H "authorization: Bearer $CAIRN_KEY"
# -> {"artifacts": [{"sha256": "...", "size": 48213, "contentType": "image/png"}]}

# Download one.
curl "$CAIRN_URL/invocations/$ID/artifacts/$SHA256" \
  -H "authorization: Bearer $CAIRN_KEY" -o chart.png
```

The response carries the content type given to `put` and is served as inert content: HTML, SVG,
XML and PDF artifacts are always sent as downloads, never rendered in place.

## Lifetime

Artifacts belong to the invocation that created them. When the invocation is purged — explicitly or
when its retention period ends — its artifacts go with it, and the HTTP routes answer `404`.

## When to use them

- Files and images produced or consumed by a step.
- Documents and model outputs that later steps of the same invocation, or a UI, read back.

Not for values carried from one invocation to the next, such as a chat history kept in state:
`ctx.artifact.get` cannot read another invocation's artifacts, and artifacts are purged with the
invocation that wrote them. Keep such values in state; large ones are stored as segments
automatically (see [Virtual objects and state](/docs/develop/state#state-api)).

On serverless platforms (`fetchHandler`), where the journal travels on every request, this matters
even more — see [Serverless](/docs/workflows/serverless).
