Writer API
This package wraps the built-in Writer API. It adds session reuse, streaming, and optional result caching. For React, see useWriter.
Before shipping, review the Production checklist for safe rendering, progress, reversible suggestions, user control, and cache freshness.
Use prepareWriter to warm a session on user intent and release it when the feature closes. See the Session lifecycle guide.
import { write } from "@web-ai-sdk/writer";
const result = await write({ input: "An inquiry to my bank about how to enable wire transfers.", context: "I'm a longstanding customer.", tone: "formal", length: "medium", onUpdate: (text) => console.log("partial", text),});
console.log(result.output, result.cached);result.output is the generated text (trimmed), or null when the input is empty. result.cached tells you whether the response came from the cache without invoking the model.
Options
Section titled “Options”interface WriteOptions { input: string; // the writing task / prompt context?: string; // per-call background info language?: string; // BCP-47; drives input/output hints when supported supportedLanguages?: readonly string[]; tone?: "formal" | "neutral" | "casual"; format?: "markdown" | "plain-text"; length?: "short" | "medium" | "long"; sharedContext?: string; monitor?: (m: CreateMonitor) => void; cache?: "session" | "local" | { get, set }; cacheKey?: string; cacheTtl?: number; // built-in shortcut TTL in ms; default 1 hour cacheRefresh?: boolean; // skip the cache read, write the fresh result onUpdate?: (text: string) => void; signal?: AbortSignal;}Returns
Section titled “Returns”interface WriteResult { output: string | null; cached: boolean;}How it works
Section titled “How it works”- Trim and cache check. Whitespace-only input short-circuits to
null. If acacheis configured and has the key, return immediately. - Cache
Writer.create()sessions by create-options. First call pays the cold start; later same-config calls reuse the warm session. - Stream
writeStreaming()when the instance supports it, falling back to one-shotwrite(). Chunks are merged (delta or cumulative) and pushed toonUpdateas the cumulative buffer. - Optionally cache the final output when you pass a
cache. Off by default. Built-in"session"/"local"entries expire after one hour by default. PasscacheTtl(milliseconds) to change the TTL per call. PasscacheRefresh: trueto skip the read and replace the cached value after a successful run. Custom{ get, set }caches own their expiry policy.
Output normalization
Section titled “Output normalization”The wrapper trims leading/trailing whitespace only, so internal markdown formatting and line breaks the model produces stay intact.
Errors and unavailability
Section titled “Errors and unavailability”The vanilla write() throws WriterUnavailableError when the API is missing:
import { write, WriterUnavailableError } from "@web-ai-sdk/writer";
try { const result = await write({ input: task });} catch (err) { if (err instanceof WriterUnavailableError) return; throw err;}AbortSignal is supported. Aborting mid-stream resolves cleanly; an opt-in result cache is not written for aborted runs.