Const// Standalone: agent that creates and reads artifacts
const builder = ai.defineAgent({
name: 'builder',
system: 'You are a code generator. Use write_artifact to create files.',
use: [artifacts()],
});
// Combined with agents middleware (session strategy)
const orchestrator = ai.defineAgent({
name: 'orchestrator',
system: 'You coordinate sub-agents and review their work.',
use: [
agents({
agents: ['researcher', 'coder'],
artifactStrategy: 'session',
}),
artifacts({ readonly: true }), // can read sub-agent artifacts
],
});
Creates a middleware that gives the model tools to interact with session artifacts, and injects an artifact listing into the system prompt.
Tools provided:
read_artifact— reads an artifact by name from the session and returns its text content.write_artifact(unlessreadonly: true) — creates or updates an artifact in the session. Artifacts are deduplicated by name: writing to an existing name replaces the artifact.System prompt injection:
An
<artifacts>block is injected into (or appended to) the system message on each generate turn, listing the names and sizes of all artifacts currently in the session. This lets the model know what's available without consuming context on the full content.This middleware is useful standalone (e.g. for a workspace-builder agent that creates files as artifacts) or combined with the
agentsmiddleware usingartifactStrategy: 'session', where sub-agent artifacts are merged into the parent session and the model accesses them via these tools.