Skip to content

Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.

feat(content): read with Claude Haiku 5.5 and write with Claude Sonnet 5.5 (#1743)

Cihane4c23dc6a9c3
diff --git a/docs/self-host/env-variables.mdx b/docs/self-host/env-variables.mdx
index 6ab2fc9cb..79264dfd0 100644
--- a/docs/self-host/env-variables.mdx
+++ b/docs/self-host/env-variables.mdx
@@ -44,6 +44,8 @@ Fed to the Express API + workers.
| `TOPIC_SUGGESTION_MODEL` | optional | LLM for topic suggestions |
| `PROMPT_SUGGESTION_MODEL` | optional | LLM for prompt suggestions |
| `COMPETITOR_SUGGESTION_MODEL` | optional | LLM for competitor suggestions |
+| `CONTENT_READ_MODEL` | optional | LLM the Content Editor reads and judges with: the brief (**Rebuild outline**) and Quality's checks. Default: `anthropic/claude-haiku-5-5` when `ANTHROPIC_API_KEY` is set, else `IMPROVE_SUGGESTION_MODEL`, then `DEFAULT_SUGGESTION_MODEL`, then `google/gemini-3-flash-preview`. `QUALITY_CHECK_MODEL`, when set, still picks Quality's model. |
+| `CONTENT_WRITE_MODEL` | optional | LLM the Content Agent writes with: pages written or rewritten whole and its suggestions. Default: `anthropic/claude-sonnet-5-5` when `ANTHROPIC_API_KEY` is set, else the same fallbacks as `CONTENT_READ_MODEL`. A Claude model must support structured outputs (Claude 4.5 models or newer). |
| `AUDIT_LLM_MODEL` | optional | LLM for the Site Audit's LLM-judged signals + AI recommendations (default: `google/gemini-3-flash-preview`) |
| `ALLOWED_ORIGINS` | ✅ | Comma-separated list of web origins allowed to call the API (for CORS) |
| `IS_CLOUD` | optional | `"true"` only for the official cloud build |
diff --git a/server/.env.example b/server/.env.example
index ef818c0cf..c364a7adf 100644
--- a/server/.env.example
+++ b/server/.env.example
@@ -67,10 +67,21 @@ COMPETITOR_SUGGESTION_MODEL=google/gemini-3-flash-preview
# answer, format and facts, as version 2 (web brief.ts). Off until the brief
# eval passes them (node src/scripts/eval-brief.js with it on).
# CONTENT_BRIEF_RULES=true
+# Content Editor models (provider/model), metered from the AI budget. The
+# read model writes the brief (Rebuild outline) and runs Quality's checks;
+# the write model is the Content Agent's: pages written or rewritten whole
+# and its suggestions. With ANTHROPIC_API_KEY set they
+# default to anthropic/claude-haiku-5-5 (read) and anthropic/claude-sonnet-5-5
+# (write); without it, to IMPROVE_SUGGESTION_MODEL, then
+# DEFAULT_SUGGESTION_MODEL, then google/gemini-3-flash-preview, as before.
+# The Improve tab keeps its own model.
+# CONTENT_READ_MODEL=
+# CONTENT_WRITE_MODEL=
# Content Editor Quality's AI checks (CE-046 on: the kind of a source's
# site the rules can't place): a small, cheap model. Metered from the same
-# AI budget as "quality". Falls back to DEFAULT_SUGGESTION_MODEL, then
-# google/gemini-3-flash-preview.
+# AI budget as "quality". Falls back to CONTENT_READ_MODEL, then
+# anthropic/claude-haiku-5-5 with ANTHROPIC_API_KEY, then
+# DEFAULT_SUGGESTION_MODEL, then google/gemini-3-flash-preview.
# QUALITY_CHECK_MODEL=
# Action Center: write each new action's tasks from its evidence (AC-004) —
# one task per page or domain, with where, why, how and done-when. Off unless
diff --git a/server/src/lib/brand-facts.js b/server/src/lib/brand-facts.js
index 6abfa67d9..0e4320d69 100644
--- a/server/src/lib/brand-facts.js
+++ b/server/src/lib/brand-facts.js
@@ -4,11 +4,13 @@ import * as cheerio from 'cheerio';
import { z } from 'zod';
import supabaseAdmin from '../config/supabase.js';
import { resolveModel } from './ai-provider.js';
+import { CONTENT_READ_MAX_TOKENS } from './content-brief.js';
import { fold } from './content-quality-claims.js';
import { sourceClassModelId } from './content-source-class.js';
import { logger } from './logger.js';
import { PlanLimitError, reserveAiBudget } from './plan-guard.js';
import { PageFetchError, fetchPage } from './safe-fetch.js';
+import { objectOptions } from './structured-output.js';
import {
PAGES as TRUST_PAGES,
SEPARATOR,
@@ -659,7 +661,7 @@ async function readFactPage({ orgId, kind, domain, text, modelId }) {
try {
const generated = await generateObject({
model: resolveModel(modelId),
- schema: answerSchema,
+ ...objectOptions(modelId, answerSchema, { maxOutputTokens: CONTENT_READ_MAX_TOKENS }),
system: SYSTEM_PROMPT,
prompt: `Page: ${kind} page of ${domain}\n\n${text}`,
abortSignal: AbortSignal.timeout(CALL_TIMEOUT_MS),
diff --git a/server/src/lib/content-agent.js b/server/src/lib/content-agent.js
index 6d857bbce..9d50cf149 100644
--- a/server/src/lib/content-agent.js
+++ b/server/src/lib/content-agent.js
@@ -4,7 +4,7 @@ import * as cheerio from 'cheerio';
import { z } from 'zod';
import supabaseAdmin from '../config/supabase.js';
import { resolveModel } from './ai-provider.js';
-import { contentBriefModelId, rebuildSources } from './content-brief.js';
+import { CONTENT_WRITE_MAX_TOKENS, contentWriteModelId, rebuildSources } from './content-brief.js';
import { inputSources, readDraftInputs } from './content-inputs.js';
import { linkAllowList, linkKey, linkTargetsPrompt, readLinkTargets } from './content-links.js';
import { shoppingGiven, shoppingPathFor, withShopping } from './shopping-links.js';
@@ -52,6 +52,7 @@ import { PlanLimitError, reserveAiBudget } from './plan-guard.js';
import { carefulLine, carefulRules } from './content-suggestion-stats.js';
import { livePageLine, readLivePage } from './content-live-page.js';
import { logger } from './logger.js';
+import { objectOptions } from './structured-output.js';
/**
* Fix open rules with agent (Content Editor CE-018, spec §8): the Content
@@ -1360,7 +1361,7 @@ async function fixWithRun({
run,
handoff = false,
}) {
- const modelId = contentBriefModelId();
+ const modelId = contentWriteModelId();
const lockedKeys = new Set(input.locked.map(passageKey));
const ruleKeys = new Set(input.rules.map((r) => r.key));
// The rules here people at the brand mostly reject its changes for
@@ -1496,7 +1497,7 @@ async function fixWithRun({
const generated = await runCall(run, budget, CALL_TIMEOUT_MS, (abortSignal) =>
generateObject({
model: resolveModel(modelId),
- schema: suggestionsSchema,
+ ...objectOptions(modelId, suggestionsSchema, { maxOutputTokens: CONTENT_WRITE_MAX_TOKENS }),
system: SYSTEM_PROMPT,
prompt: buildFixPrompt({
brand,
diff --git a/server/src/lib/content-agent.test.js b/server/src/lib/content-agent.test.js
index 5070f5f31..9248df11e 100644
--- a/server/src/lib/content-agent.test.js
+++ b/server/src/lib/content-agent.test.js
@@ -44,7 +44,9 @@ class NoObjectGeneratedError extends Error {
return err instanceof NoObjectGeneratedError;
}
}
-vi.mock('ai', () => ({
+// The real schema helpers: a Claude call's schema is built with them (structured-output.js).
+vi.mock('ai', async (importOriginal) => ({
+ ...(await importOriginal()),
generateObject: (...args) => generateObject(...args),
NoObjectGeneratedError,
}));
@@ -166,7 +168,11 @@ beforeEach(() => {
generateObject.mockReset();
reserveAiBudget.mockReset().mockImplementation(realGuard.reserveAiBudget);
readLinkTargets.mockReset().mockResolvedValue(NO_LINK_TARGETS);
+ // The model as a server without an Anthropic key picks it, whatever the shell has.
+ vi.stubEnv('ANTHROPIC_API_KEY', '');
+ vi.stubEnv('CONTENT_WRITE_MODEL', '');
});
+afterEach(() => vi.unstubAllEnvs());
describe('the page as the agent reads it', () => {
it('reads headings and paragraphs in order, as the editor does', () => {
@@ -2030,6 +2036,35 @@ describe('fixDraftRules', () => {
expect(count).toBe(1);
});
+ it('writes its suggestions with Claude Sonnet with an Anthropic key, charged at its price', async () => {
+ vi.stubEnv('ANTHROPIC_API_KEY', 'test-key');
+ generateObject.mockResolvedValue({
+ object: { suggestions: [suggestion()] },
+ usage: { inputTokens: 900, outputTokens: 200 },
+ });
+ await fixDraftRules({ draftId: DRAFT, brandId: BRAND, input: input({ sources: SOURCES }) });
+
+ const sonnet = 'anthropic/claude-sonnet-5-5';
+ expect(reserveAiBudget).toHaveBeenCalledWith(
+ 'org-1',
+ expect.objectContaining({ feature: 'content', modelId: sonnet }),
+ );
+ const call = generateObject.mock.calls[0][0];
+ expect(call.model).toBe(`model:${sonnet}`);
+ expect(call.maxOutputTokens).toBe(32_000);
+ expect(call.providerOptions).toEqual({ anthropic: { structuredOutputMode: 'outputFormat' } });
+ for (const key of ['temperature', 'topP', 'topK']) expect(call).not.toHaveProperty(key);
+ // At most 8 suggestions, told in words: Claude takes no maxItems.
+ expect(call.schema.jsonSchema.properties.suggestions).toMatchObject({
+ description: '(at most 8 items)',
+ });
+ expect(call.schema.jsonSchema.properties.suggestions).not.toHaveProperty('maxItems');
+ // $2 / $10 per million: 900 in and 200 out.
+ const usage = writes.find((w) => w.table === 'prompt_improve_usage');
+ expect(usage.value).toMatchObject({ model: sonnet, input_tokens: 900, output_tokens: 200 });
+ expect(usage.value.cost_usd).toBeCloseTo(0.0038, 6);
+ });
+
it('charges the budget, asks the model and keeps one batch for review', async () => {
generateObject.mockResolvedValue({
object: {
diff --git a/server/src/lib/content-brief.js b/server/src/lib/content-brief.js
index f6ee4811c..627ef5c57 100644
--- a/server/src/lib/content-brief.js
+++ b/server/src/lib/content-brief.js
@@ -33,6 +33,7 @@ import {
todayText,
} from './content-sources.js';
import { reserveAiBudget } from './plan-guard.js';
+import { objectOptions } from './structured-output.js';
/**
* The Content Editor's brief (CE-007, spec §10): Rebuild outline writes a
@@ -44,7 +45,11 @@ import { reserveAiBudget } from './plan-guard.js';
* (spec §3).
*/
-/** The model, as the Improve tab's. */
+/**
+ * The Improve tab's model, which the Content Editor ran on before it had
+ * its own (contentReadModelId, contentWriteModelId), and still runs on
+ * without an Anthropic key.
+ */
export function contentBriefModelId() {
return (
process.env.IMPROVE_SUGGESTION_MODEL ||
@@ -53,6 +58,44 @@ export function contentBriefModelId() {
);
}
+/** The Content Editor's models with ANTHROPIC_API_KEY set: Haiku reads, Sonnet writes. */
+export const CONTENT_READ_CLAUDE = 'anthropic/claude-haiku-5-5';
+export const CONTENT_WRITE_CLAUDE = 'anthropic/claude-sonnet-5-5';
+
+/**
+ * The model the Content Editor reads and judges with: the brief (Rebuild
+ * outline) and Quality's reads (content-source-class.js). CONTENT_READ_MODEL,
+ * else Claude Haiku with an Anthropic key, else `fallback`, the model it ran
+ * on before: a server without the key runs as it did.
+ */
+export function contentReadModelId(fallback = contentBriefModelId) {
+ return (
+ process.env.CONTENT_READ_MODEL ||
+ (process.env.ANTHROPIC_API_KEY ? CONTENT_READ_CLAUDE : fallback())
+ );
+}
+
+/**
+ * The model the Content Agent writes with: whatever goes into the page (a
+ * page written or rewritten whole, the cut pass, its suggestions).
+ * CONTENT_WRITE_MODEL, else Claude Sonnet with an Anthropic key, else the
+ * model it ran on before.
+ */
+export function contentWriteModelId() {
+ return (
+ process.env.CONTENT_WRITE_MODEL ||
+ (process.env.ANTHROPIC_API_KEY ? CONTENT_WRITE_CLAUDE : contentBriefModelId())
+ );
+}
+
+/**
+ * The most a read's and a write's answer may take, sent to Claude only
+ * (structured-output.js): its thinking counts toward it. A whole page of
+ * BRIEF_LENGTH_MAX characters is about 17k tokens as HTML in JSON.
+ */
+export const CONTENT_READ_MAX_TOKENS = 16_000;
+export const CONTENT_WRITE_MAX_TOKENS = 32_000;
+
/** How long the model may take: the dashboard waits for it. */
const CALL_TIMEOUT_MS = 60_000;
@@ -563,7 +606,7 @@ async function briefWithRun({
targets,
run,
}) {
- const modelId = contentBriefModelId();
+ const modelId = contentReadModelId();
// Held on the budget before the call, so calls at once can't pass it.
const budget = await reserveAiBudget(brand.organization_id, {
feature: 'content',
@@ -584,7 +627,7 @@ async function briefWithRun({
generated = await runCall(run, budget, CALL_TIMEOUT_MS, (abortSignal) =>
generateObject({
model: resolveModel(modelId),
- schema: briefSchema(),
+ ...objectOptions(modelId, briefSchema(), { maxOutputTokens: CONTENT_READ_MAX_TOKENS }),
system: briefSystemPrompt(),
prompt: buildBriefPrompt({
brand,
diff --git a/server/src/lib/content-brief.test.js b/server/src/lib/content-brief.test.js
index 1fadb0ec5..3e2ab9eef 100644
--- a/server/src/lib/content-brief.test.js
+++ b/server/src/lib/content-brief.test.js
@@ -48,7 +48,9 @@ class NoObjectGeneratedError extends Error {
return err instanceof NoObjectGeneratedError;
}
}
-vi.mock('ai', () => ({
+// The real schema helpers: a Claude call's schema is built with them (structured-output.js).
+vi.mock('ai', async (importOriginal) => ({
+ ...(await importOriginal()),
generateObject: (...args) => generateObject(...args),
NoObjectGeneratedError,
}));
@@ -88,6 +90,9 @@ const {
briefForWriter,
briefNumbered,
briefRules,
+ contentBriefModelId,
+ contentReadModelId,
+ contentWriteModelId,
rebuildDraftBrief,
rebuildSources,
} = await import('./content-brief.js');
@@ -163,6 +168,56 @@ beforeEach(() => {
usage: { inputTokens: 1000, outputTokens: 500 },
});
reserveAiBudget.mockReset().mockImplementation(realGuard.reserveAiBudget);
+ // The models as a server without an Anthropic key picks them, whatever the shell has.
+ vi.stubEnv('ANTHROPIC_API_KEY', '');
+ vi.stubEnv('CONTENT_READ_MODEL', '');
+ vi.stubEnv('CONTENT_WRITE_MODEL', '');
+});
+afterEach(() => vi.unstubAllEnvs());
+
+const GEMINI = 'google/gemini-3-flash-preview';
+const HAIKU = 'anthropic/claude-haiku-5-5';
+const SONNET = 'anthropic/claude-sonnet-5-5';
+
+describe("the Content Editor's models", () => {
+ const ids = () => ({
+ read: contentReadModelId(),
+ write: contentWriteModelId(),
+ improve: contentBriefModelId(),
+ });
+
+ it('stay on the model they had, the Improve tab’s, without an Anthropic key', () => {
+ vi.stubEnv('IMPROVE_SUGGESTION_MODEL', '');
+ vi.stubEnv('DEFAULT_SUGGESTION_MODEL', '');
+ expect(ids()).toEqual({ read: GEMINI, write: GEMINI, improve: GEMINI });
+ vi.stubEnv('IMPROVE_SUGGESTION_MODEL', 'openai/gpt-5-mini');
+ expect(ids()).toEqual({
+ read: 'openai/gpt-5-mini',
+ write: 'openai/gpt-5-mini',
+ improve: 'openai/gpt-5-mini',
+ });
+ });
+
+ it('read with Claude Haiku and write with Claude Sonnet with an Anthropic key; Improve keeps its own', () => {
+ vi.stubEnv('ANTHROPIC_API_KEY', 'test-key');
+ vi.stubEnv('IMPROVE_SUGGESTION_MODEL', GEMINI);
+ expect(ids()).toEqual({ read: HAIKU, write: SONNET, improve: GEMINI });
+ });
+
+ it('take CONTENT_READ_MODEL and CONTENT_WRITE_MODEL first, with the key or without it', () => {
+ vi.stubEnv('CONTENT_READ_MODEL', 'google/gemini-3-flash');
+ vi.stubEnv('CONTENT_WRITE_MODEL', 'openai/gpt-5-mini');
+ const set = { read: 'google/gemini-3-flash', write: 'openai/gpt-5-mini' };
+ expect(ids()).toMatchObject(set);
+ vi.stubEnv('ANTHROPIC_API_KEY', 'test-key');
+ expect(ids()).toMatchObject(set);
+ });
+
+ it('read with the fallback a caller gives when there is no key', () => {
+ expect(contentReadModelId(() => 'openai/gpt-5-mini')).toBe('openai/gpt-5-mini');
+ vi.stubEnv('ANTHROPIC_API_KEY', 'test-key');
+ expect(contentReadModelId(() => 'openai/gpt-5-mini')).toBe(HAIKU);
+ });
});
describe('rebuildDraftBrief', () => {
@@ -221,16 +276,40 @@ describe('rebuildDraftBrief', () => {
});
});
+ it('writes the brief with Claude Haiku with an Anthropic key: structured output, a token cap, no sampling', async () => {
+ vi.stubEnv('ANTHROPIC_API_KEY', 'test-key');
+ await rebuildDraftBrief({ draftId: DRAFT, brandId: BRAND, sources: sources() });
+
+ expect(reserveAiBudget).toHaveBeenCalledWith('o1', {
+ feature: 'content',
+ modelId: HAIKU,
+ draftId: DRAFT,
+ });
+ const call = generateObject.mock.calls[0][0];
+ expect(call.model).toBe(`model:${HAIKU}`);
+ expect(call.maxOutputTokens).toBe(16_000);
+ expect(call.providerOptions).toEqual({ anthropic: { structuredOutputMode: 'outputFormat' } });
+ for (const key of ['temperature', 'topP', 'topK']) expect(call).not.toHaveProperty(key);
+ // A schema Claude takes: none of the keywords it turns away.
+ expect(call.schema.jsonSchema).toMatchObject({ type: 'object', additionalProperties: false });
+ expect(JSON.stringify(call.schema.jsonSchema)).not.toMatch(
+ /"(\$schema|maxItems|maxLength|minLength|minimum|maximum)"/,
+ );
+ });
+
it('writes the brief from the kept sources, charges AI usage, and saves it on the draft', async () => {
const brief = await rebuildDraftBrief({ draftId: DRAFT, brandId: BRAND, sources: sources() });
expect(reserveAiBudget).toHaveBeenCalledWith('o1', {
feature: 'content',
- modelId: 'google/gemini-3-flash-preview',
+ modelId: GEMINI,
draftId: DRAFT,
});
const call = generateObject.mock.calls[0][0];
- expect(call.model).toBe('model:google/gemini-3-flash-preview');
+ expect(call.model).toBe(`model:${GEMINI}`);
+ // Another provider's call is sent as it was: no token cap, no provider options.
+ expect(call).not.toHaveProperty('maxOutputTokens');
+ expect(call).not.toHaveProperty('providerOptions');
expect(call.prompt).toContain('- Best espresso machines under $500?');
expect(call.prompt).toContain(
'"Best espresso machine under 500" (perplexity-web, copilot-web)',
diff --git a/server/src/lib/content-quality-claims.js b/server/src/lib/content-quality-claims.js
index 9d756c26d..edb2b318e 100644
--- a/server/src/lib/content-quality-claims.js
+++ b/server/src/lib/content-quality-claims.js
@@ -4,6 +4,7 @@ import * as cheerio from 'cheerio';
import { z } from 'zod';
import supabaseAdmin from '../config/supabase.js';
import { resolveModel } from './ai-provider.js';
+import { CONTENT_READ_MAX_TOKENS } from './content-brief.js';
import {
PAGE_READS_PER_DAY,
QUALITY_CALLS_PER_DAY,
@@ -12,6 +13,7 @@ import {
import { SOURCE_READS_PER_DAY, sourceClassModelId } from './content-source-class.js';
import { logger } from './logger.js';
import { reserveAiBudget } from './plan-guard.js';
+import { objectOptions } from './structured-output.js';
/**
* The claims in a draft's own words (Quality CE-045, spec §3: "Claim
@@ -184,7 +186,7 @@ async function readCall({ orgId, draftId, modelId, paragraphs }) {
try {
const generated = await generateObject({
model: resolveModel(modelId),
- schema: answerSchema,
+ ...objectOptions(modelId, answerSchema, { maxOutputTokens: CONTENT_READ_MAX_TOKENS }),
system: SYSTEM_PROMPT,
prompt: buildClaimsPrompt(paragraphs),
abortSignal: AbortSignal.timeout(CALL_TIMEOUT_MS),
diff --git a/server/src/lib/content-quality-page.js b/server/src/lib/content-quality-page.js
index 5ef084d28..d2a00c5a8 100644
--- a/server/src/lib/content-quality-page.js
+++ b/server/src/lib/content-quality-page.js
@@ -4,6 +4,7 @@ import * as cheerio from 'cheerio';
import { z } from 'zod';
import supabaseAdmin from '../config/supabase.js';
import { resolveModel } from './ai-provider.js';
+import { CONTENT_READ_MAX_TOKENS } from './content-brief.js';
import { SITE_TYPE_MEANINGS, SITE_TYPE_NAMES, pageTypeFormat } from './content-page-types.js';
import {
PAGE_READS_PER_DAY,
@@ -14,6 +15,7 @@ import { fold, ownText, paragraphHash, plain } from './content-quality-claims.js
import { SOURCE_READS_PER_DAY, sourceClassModelId } from './content-source-class.js';
import { logger } from './logger.js';
import { PlanLimitError, reserveAiBudget } from './plan-guard.js';
+import { objectOptions } from './structured-output.js';
/**
* A page's visual proof spots and proof asks (Content Editor Quality spec
@@ -695,7 +697,7 @@ async function readOnce({
try {
const generated = await generateObject({
model: resolveModel(modelId),
- schema: answerSchema,
+ ...objectOptions(modelId, answerSchema, { maxOutputTokens: CONTENT_READ_MAX_TOKENS }),
system: SYSTEM_PROMPT,
prompt: buildPagePrompt({ outline, siteType, pageType, brandName, facts }),
abortSignal: AbortSignal.timeout(CALL_TIMEOUT_MS),
diff --git a/server/src/lib/content-quality-page.test.js b/server/src/lib/content-quality-page.test.js
index 6792b6224..46a362d82 100644
--- a/server/src/lib/content-quality-page.test.js
+++ b/server/src/lib/content-quality-page.test.js
@@ -1,4 +1,4 @@
-import { beforeEach, describe, expect, it, vi } from 'vitest';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
/**
* A page's visual proof spots and proof asks (Quality CE-049): read by the
@@ -120,6 +120,7 @@ beforeEach(() => {
keep.mockReset();
generateObject.mockReset().mockResolvedValue(answer());
});
+afterEach(() => vi.unstubAllEnvs());
const read = (over = {}) =>
readPage({
@@ -400,6 +401,26 @@ describe('readPage', () => {
expect(result.proofs.map((p) => p.block)).toEqual([SEAMS, AWARD]);
});
+ it('reads with Claude Haiku with an Anthropic key, in a schema Claude takes, and keeps its id', async () => {
+ vi.stubEnv('QUALITY_CHECK_MODEL', '');
+ vi.stubEnv('CONTENT_READ_MODEL', '');
+ vi.stubEnv('ANTHROPIC_API_KEY', 'test-key');
+ await read();
+ const [call] = generateObject.mock.calls[0];
+ expect(call.model).toEqual({ id: 'anthropic/claude-haiku-5-5' });
+ expect(call.maxOutputTokens).toBe(16_000);
+ expect(call.providerOptions).toEqual({ anthropic: { structuredOutputMode: 'outputFormat' } });
+ for (const key of ['temperature', 'topP', 'topK']) expect(call).not.toHaveProperty(key);
+ const schema = call.schema.jsonSchema;
+ expect(JSON.stringify(schema)).not.toMatch(/"(\$schema|minimum|maximum|maxItems|oneOf)"/);
+ // A check's quote or none: a closed object, or null.
+ expect(schema.properties.balance.anyOf).toEqual([
+ expect.objectContaining({ type: 'object', additionalProperties: false }),
+ { type: 'null' },
+ ]);
+ expect(upserts[0].row.model).toBe('anthropic/claude-haiku-5-5');
+ });
+
it('reads nothing for a short page, an unchanged one, or within the minute', async () => {
expect(
await read({ html: '<h1>Short</h1><p>Only a few words on this page.</p>' }),
diff --git a/server/src/lib/content-source-class.js b/server/src/lib/content-source-class.js
index e4cdf3496..e4099f42e 100644
--- a/server/src/lib/content-source-class.js
+++ b/server/src/lib/content-source-class.js
@@ -2,9 +2,11 @@ import { NoObjectGeneratedError, generateObject } from 'ai';
import { z } from 'zod';
import supabaseAdmin from '../config/supabase.js';
import { resolveModel } from './ai-provider.js';
+import { CONTENT_READ_MAX_TOKENS, contentReadModelId } from './content-brief.js';
import { QUALITY_CALLS_PER_DAY, qualityCallsToday } from './content-quality-calls.js';
import { logger } from './logger.js';
import { PlanLimitError, reserveAiBudget } from './plan-guard.js';
+import { objectOptions } from './structured-output.js';
/**
* The kind of source a site is (Quality CE-046, part b), read by the model
@@ -46,11 +48,18 @@ export const SOURCE_CLASS_HOLD_USD = 0.001;
export const SOURCE_READS_PER_DAY = 40;
const CALL_TIMEOUT_MS = 30_000;
+/**
+ * The model Quality's reads use (the sources' kinds, the claims, the page
+ * read, the brand's facts): QUALITY_CHECK_MODEL, else the Content Editor's
+ * read model (contentReadModelId: Claude Haiku with an Anthropic key), else
+ * the one they ran on before.
+ */
export function sourceClassModelId() {
return (
process.env.QUALITY_CHECK_MODEL ||
- process.env.DEFAULT_SUGGESTION_MODEL ||
- 'google/gemini-3-flash-preview'
+ contentReadModelId(
+ () => process.env.DEFAULT_SUGGESTION_MODEL || 'google/gemini-3-flash-preview',
+ )
);
}
@@ -138,7 +147,7 @@ async function readSite({ orgId, draftId, site, modelId }) {
try {
const generated = await generateObject({
model: resolveModel(modelId),
- schema: answerSchema,
+ ...objectOptions(modelId, answerSchema, { maxOutputTokens: CONTENT_READ_MAX_TOKENS }),
system: SYSTEM_PROMPT,
prompt: buildSourceClassPrompt(site),
abortSignal: AbortSignal.timeout(CALL_TIMEOUT_MS),
diff --git a/server/src/lib/content-source-class.test.js b/server/src/lib/content-source-class.test.js
index b81d10ede..480d61fc7 100644
--- a/server/src/lib/content-source-class.test.js
+++ b/server/src/lib/content-source-class.test.js
@@ -1,4 +1,4 @@
-import { beforeEach, describe, expect, it, vi } from 'vitest';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
/**
* The kind of source a site the editor's rules can't place is (Quality
@@ -70,6 +70,7 @@ const {
linkedSites,
readSourceClasses,
siteOf,
+ sourceClassModelId,
} = await import('./content-source-class.js');
const { QUALITY_CALLS_PER_DAY } = await import('./content-quality-calls.js');
@@ -83,6 +84,10 @@ beforeEach(() => {
charge.mockReset();
release.mockReset();
keep.mockReset();
+ // The models as a server without an Anthropic key picks them, whatever the shell has.
+ for (const name of ['ANTHROPIC_API_KEY', 'CONTENT_READ_MODEL', 'QUALITY_CHECK_MODEL']) {
+ vi.stubEnv(name, '');
+ }
// Each site in its own call: answered by the site the prompt names.
generateObject.mockReset().mockImplementation(async ({ prompt }) => ({
object: { kind: KINDS[prompt.replace('Site: ', '')] ?? 'unknown' },
@@ -90,6 +95,8 @@ beforeEach(() => {
}));
});
+afterEach(() => vi.unstubAllEnvs());
+
const KINDS = { 'coffee-blog.example': 'weak', 'roaster.example': 'primary' };
const asked = () => generateObject.mock.calls.map(([call]) => call.prompt);
@@ -137,7 +144,48 @@ describe('siteOf and linkedSites', () => {
});
});
+describe('sourceClassModelId: the model of Quality’s reads', () => {
+ it('stays on the default suggestion model without an Anthropic key (never Improve’s)', () => {
+ vi.stubEnv('DEFAULT_SUGGESTION_MODEL', '');
+ vi.stubEnv('IMPROVE_SUGGESTION_MODEL', 'openai/gpt-5-mini');
+ expect(sourceClassModelId()).toBe('google/gemini-3-flash-preview');
+ vi.stubEnv('DEFAULT_SUGGESTION_MODEL', 'google/gemini-3-flash');
+ expect(sourceClassModelId()).toBe('google/gemini-3-flash');
+ });
+
+ it('reads with Claude Haiku with an Anthropic key, or CONTENT_READ_MODEL', () => {
+ vi.stubEnv('ANTHROPIC_API_KEY', 'test-key');
+ expect(sourceClassModelId()).toBe('anthropic/claude-haiku-5-5');
+ vi.stubEnv('CONTENT_READ_MODEL', 'google/gemini-3-flash');
+ expect(sourceClassModelId()).toBe('google/gemini-3-flash');
+ });
+
+ it('keeps QUALITY_CHECK_MODEL first, when it is set', () => {
+ vi.stubEnv('ANTHROPIC_API_KEY', 'test-key');
+ vi.stubEnv('CONTENT_READ_MODEL', 'google/gemini-3-flash');
+ vi.stubEnv('QUALITY_CHECK_MODEL', 'openai/gpt-5-mini');
+ expect(sourceClassModelId()).toBe('openai/gpt-5-mini');
+ });
+});
+
describe('readSourceClasses', () => {
+ it('reads a site with Claude Haiku with an Anthropic key: structured output, a token cap, no sampling', async () => {
+ vi.stubEnv('ANTHROPIC_API_KEY', 'test-key');
+ await read(['roaster.example']);
+ const [call] = generateObject.mock.calls[0];
+ expect(call.model).toEqual({ id: 'anthropic/claude-haiku-5-5' });
+ expect(reserves[0].modelId).toBe('anthropic/claude-haiku-5-5');
+ expect(call.maxOutputTokens).toBe(16_000);
+ expect(call.providerOptions).toEqual({ anthropic: { structuredOutputMode: 'outputFormat' } });
+ for (const key of ['temperature', 'topP', 'topK']) expect(call).not.toHaveProperty(key);
+ expect(call.schema.jsonSchema).toEqual({
+ type: 'object',
+ properties: { kind: { type: 'string', enum: ['primary', 'secondary', 'weak', 'unknown'] } },
+ required: ['kind'],
+ additionalProperties: false,
+ });
+ });
+
it('reads each site not kept in its own call, keeps them, and holds the budget as quality for each', async () => {
kept = [{ host: 'news.example', kind: 'secondary' }];
const result = await read(['news.example', 'coffee-blog.example', 'roaster.example']);
diff --git a/server/src/lib/content-writer.js b/server/src/lib/content-writer.js
index 0cfda9146..67bf01036 100644
--- a/server/src/lib/content-writer.js
+++ b/server/src/lib/content-writer.js
@@ -24,10 +24,11 @@ import {
WRITING_STALE_MS,
} from './content-agent.js';
import {
+ CONTENT_WRITE_MAX_TOKENS,
WRITING_GUARDRAIL,
briefForWriter,
briefRulesOn,
- contentBriefModelId,
+ contentWriteModelId,
rebuildSources,
} from './content-brief.js';
import { inputSources, readDraftInputs } from './content-inputs.js';
@@ -82,6 +83,7 @@ import {
import { getLanguageName } from './languages.js';
import { logger } from './logger.js';
import { reserveAiBudget } from './plan-guard.js';
+import { objectOptions } from './structured-output.js';
/**
* "Agent" in the Content Editor's Start (CE-015, spec §5): the Content Agent
@@ -572,7 +574,7 @@ export async function startDraftWriting({ draftId, brandId, input, rewrite = fal
.eq('id', brandId)
.single();
if (brandError) throw brandError;
- const modelId = contentBriefModelId();
+ const modelId = contentWriteModelId();
// Held on the budget now, for the run below: starts at once can't pass it.
const budget = await reserveAiBudget(brand.organization_id, {
feature: 'content',
@@ -617,8 +619,8 @@ export async function startDraftWriting({ draftId, brandId, input, rewrite = fal
// it, at once; with its claim, the one a Stop on it lets go of.
const held = { ...draft, agent_state: 'writing', agent_state_at: claimedAt };
const run = await startWritingRun(held, input, rewrite);
- runDraftWriting({ draft: held, brand, model, input, budget, rewrite, run }).catch((err) =>
- logger.error({ err, draftId }, '[content-writer] run failed'),
+ runDraftWriting({ draft: held, brand, model, modelId, input, budget, rewrite, run }).catch(
+ (err) => logger.error({ err, draftId }, '[content-writer] run failed'),
);
} catch (err) {
// Not started: the hold goes.
@@ -668,6 +670,7 @@ async function rewritePage({
shoppingPath,
brand,
model,
+ modelId,
input,
budget,
numbered,
@@ -695,7 +698,7 @@ async function rewritePage({
const { object, usage } = await runCall(run, budget, CALL_TIMEOUT_MS, (abortSignal) =>
generateObject({
model,
- schema: rewriteSchema,
+ ...objectOptions(modelId, rewriteSchema, { maxOutputTokens: CONTENT_WRITE_MAX_TOKENS }),
system: rewriteSystemPrompt(),
prompt: buildWritePrompt({
brand,
@@ -713,7 +716,7 @@ async function rewritePage({
if (!isPageHtml(object.html)) throw new Error('The agent answered with no page in HTML');
// Well past its Length: one more pass that only cuts.
const answer = ruleKeys.has('length')
- ? await cutToLength({ answer: object, page, length, brand, draft, model, run })
+ ? await cutToLength({ answer: object, page, length, brand, draft, model, modelId, run })
: object;
await advanceRun(run, 3);
@@ -796,13 +799,13 @@ async function rewritePage({
* cut is taken only when it is shorter and keeps every kept line; else, or
* when it fails, the first rewrite stands (logged). A stop goes through.
*/
-async function cutToLength({ answer, page, length, brand, draft, model, run }) {
+async function cutToLength({ answer, page, length, brand, draft, model, modelId, run }) {
const total = ownText({ ...page, html: answer.html }) + page.keptChars;
const max = Math.round(length * (1 + LENGTH_TOLERANCE));
if (total <= max + length * CUT_PASS_OVER) return answer;
const budget = await reserveAiBudget(brand.organization_id, {
feature: 'content',
- modelId: contentBriefModelId(),
+ modelId,
draftId: draft.id,
}).catch((err) => {
logger.info(
@@ -816,7 +819,7 @@ async function cutToLength({ answer, page, length, brand, draft, model, run }) {
const { object, usage } = await runCall(run, budget, CALL_TIMEOUT_MS, (abortSignal) =>
generateObject({
model,
- schema: rewriteSchema,
+ ...objectOptions(modelId, rewriteSchema, { maxOutputTokens: CONTENT_WRITE_MAX_TOKENS }),
system: rewriteSystemPrompt(),
prompt: cutPrompt({ html: answer.html, page, total, length, max }),
abortSignal,
@@ -871,12 +874,15 @@ Write in the page's own language.`;
* The run itself: the page written, or the suggestions left, and the draft
* In review; 'failed' when the model fails or leaves nothing to review. The
* model's answer is charged to the hold Start took on the budget
- * (reserveAiBudget); a run that ends before one lets the hold go.
+ * (reserveAiBudget); a run that ends before one lets the hold go. `modelId`
+ * is the `model`'s, for its call settings (structured-output.js) and the
+ * cut pass's hold.
*/
export async function runDraftWriting({
draft,
brand,
model,
+ modelId = contentWriteModelId(),
input,
budget,
rewrite = false,
@@ -886,7 +892,16 @@ export async function runDraftWriting({
// before it answers, so Stop finds it at once; Stop aborts the call.
const run = started ?? (await startWritingRun(draft, input, rewrite));
try {
- const result = await writeWithRun({ draft, brand, model, input, budget, rewrite, run });
+ const result = await writeWithRun({
+ draft,
+ brand,
+ model,
+ modelId,
+ input,
+ budget,
+ rewrite,
+ run,
+ });
await finishRun(
run,
'done',
@@ -938,7 +953,7 @@ function startWritingRun(draft, input, rewrite) {
}
/** The run itself, step by step: nothing is kept once it is stopped. */
-async function writeWithRun({ draft, brand, model, input, budget, rewrite, run }) {
+async function writeWithRun({ draft, brand, model, modelId, input, budget, rewrite, run }) {
await advanceRun(run, 1);
const [prompts, inputRow, opportunityRes, shoppingPath] = await Promise.all([
readPrompts(draft.brand_id, input.sources.promptIds),
@@ -1024,6 +1039,7 @@ async function writeWithRun({ draft, brand, model, input, budget, rewrite, run }
shoppingPath,
brand,
model,
+ modelId,
input,
budget,
numbered,
@@ -1039,7 +1055,7 @@ async function writeWithRun({ draft, brand, model, input, budget, rewrite, run }
const { object, usage } = await runCall(run, budget, CALL_TIMEOUT_MS, (abortSignal) =>
generateObject({
model,
- schema: pageSchema,
+ ...objectOptions(modelId, pageSchema, { maxOutputTokens: CONTENT_WRITE_MAX_TOKENS }),
system: writeSystemPrompt(),
prompt: buildWritePrompt({
brand,
@@ -1108,7 +1124,7 @@ async function writeWithRun({ draft, brand, model, input, budget, rewrite, run }
const { object, usage } = await runCall(run, budget, CALL_TIMEOUT_MS, (abortSignal) =>
generateObject({
model,
- schema: suggestionsSchema,
+ ...objectOptions(modelId, suggestionsSchema, { maxOutputTokens: CONTENT_WRITE_MAX_TOKENS }),
system: suggestSystemPrompt(),
prompt: buildWritePrompt({
brand,
diff --git a/server/src/lib/content-writer.test.js b/server/src/lib/content-writer.test.js
index 0a6d5b208..45a9d02ad 100644
--- a/server/src/lib/content-writer.test.js
+++ b/server/src/lib/content-writer.test.js
@@ -64,7 +64,9 @@ class NoObjectGeneratedError extends Error {
return err instanceof NoObjectGeneratedError;
}
}
-vi.mock('ai', () => ({
+// The real schema helpers: a Claude call's schema is built with them (structured-output.js).
+vi.mock('ai', async (importOriginal) => ({
+ ...(await importOriginal()),
generateObject: (...args) => generateObject(...args),
NoObjectGeneratedError,
}));
@@ -188,7 +190,22 @@ beforeEach(() => {
hasOpenSuggestions.mockReset().mockResolvedValue(false);
fixUnderWay.mockReset().mockResolvedValue(false);
stopIfStopped.mockReset().mockResolvedValue(undefined);
+ // The model as a server without an Anthropic key picks it, whatever the shell has.
+ vi.stubEnv('ANTHROPIC_API_KEY', '');
+ vi.stubEnv('CONTENT_WRITE_MODEL', '');
});
+afterEach(() => vi.unstubAllEnvs());
+
+const SONNET = 'anthropic/claude-sonnet-5-5';
+/** A call as Claude 5.5 takes it: structured output, a token cap, no sampling. */
+const expectClaudeCall = (call) => {
+ expect(call.maxOutputTokens).toBe(32_000);
+ expect(call.providerOptions).toEqual({ anthropic: { structuredOutputMode: 'outputFormat' } });
+ for (const key of ['temperature', 'topP', 'topK']) expect(call).not.toHaveProperty(key);
+ expect(JSON.stringify(call.schema.jsonSchema)).not.toMatch(
+ /"(\$schema|maxItems|maxLength|minLength|minimum|maximum)"/,
+ );
+};
const draftUpdates = () =>
writes.filter((w) => w.table === 'content_drafts' && w.kind === 'update').map((w) => w.value);
@@ -346,6 +363,25 @@ describe('startDraftWriting', () => {
await vi.waitFor(() => expect(draftUpdates()).toHaveLength(2));
});
+ it('writes with Claude Sonnet with an Anthropic key', async () => {
+ vi.stubEnv('ANTHROPIC_API_KEY', 'test-key');
+ await startDraftWriting({ draftId: DRAFT, brandId: BRAND, input: input() });
+ await vi.waitFor(() => expect(draftUpdates()).toHaveLength(2));
+ expect(resolveModel).toHaveBeenLastCalledWith(SONNET);
+ expect(reserveAiBudget).toHaveBeenCalledWith(
+ 'org-1',
+ expect.objectContaining({ feature: 'content', modelId: SONNET }),
+ );
+ const call = generateObject.mock.calls[0][0];
+ expect(call.model).toBe(`model:${SONNET}`);
+ expectClaudeCall(call);
+ // Two to twelve sections, told in words: Claude takes minItems 0 or 1 only.
+ expect(call.schema.jsonSchema.properties.sections).toMatchObject({
+ minItems: 1,
+ description: '(at least 2 items, at most 12 items)',
+ });
+ });
+
it('says it reads as many rules as the editor counts, when it says so', async () => {
await startDraftWriting({ draftId: DRAFT, brandId: BRAND, input: input({ counted: 7 }) });
const run = writes.find((w) => w.table === 'content_agent_runs' && w.kind === 'insert').value;
@@ -1200,6 +1236,29 @@ describe('Rewrite the page to these rules', () => {
expect(row().after_chars).toBe(textLength(row().after_text));
});
+ it('rewrites and cuts with the model it was started with, held at its own id', async () => {
+ answers(long, short);
+ await runDraftWriting({
+ draft: existing,
+ brand,
+ model: `model:${SONNET}`,
+ modelId: SONNET,
+ input: input({ locked, ...toLength() }),
+ budget,
+ rewrite: true,
+ });
+ expect(generateObject).toHaveBeenCalledTimes(2);
+ for (const [call] of generateObject.mock.calls) {
+ expect(call.model).toBe(`model:${SONNET}`);
+ expectClaudeCall(call);
+ }
+ expect(reserveAiBudget).toHaveBeenCalledWith('org-1', {
+ feature: 'content',
+ modelId: SONNET,
+ draftId: DRAFT,
+ });
+ });
+
it('keeps the first rewrite when the cut is no shorter, drops a kept line, fails or has no budget', async () => {
answers(long, long);
await run(toLength());
diff --git a/server/src/lib/improve-cost.js b/server/src/lib/improve-cost.js
index bf7cfa40e..16b2bf813 100644
--- a/server/src/lib/improve-cost.js
+++ b/server/src/lib/improve-cost.js
@@ -9,7 +9,9 @@
* doesn't have, or every model with the flag off. Update it when a provider
* changes its prices or a new model is configured; a model missing from both
* is charged at FALLBACK_PRICE, deliberately expensive, so an unknown model
- * can only use the budget up faster, never overspend it.
+ * can only use the budget up faster, never overspend it. A model here priced
+ * higher for a long prompt has its `long` rate, for a call whose input tokens
+ * pass `over`.
*/
import { syncedPriceFor } from './model-price-cache.js';
@@ -19,6 +21,13 @@ export const MODEL_PRICES = {
'google/gemini-3-flash': { input: 0.5, output: 3 },
'openai/gpt-5-mini': { input: 0.25, output: 2 },
'anthropic/claude-haiku-4-5': { input: 1, output: 5 },
+ // The Content Editor's with an Anthropic key (content-brief.js): Haiku reads, Sonnet writes.
+ 'anthropic/claude-haiku-5-5': {
+ input: 0.1,
+ output: 0.5,
+ long: { over: 100_000, input: 0.5, output: 2.5 },
+ },
+ 'anthropic/claude-sonnet-5-5': { input: 2, output: 10 },
};
export const FALLBACK_PRICE = { input: 5, output: 25 };
@@ -40,7 +49,8 @@ export function priceFor(modelId, { promptTokens = 0 } = {}) {
const synced = syncedPriceFor(modelId, { promptTokens });
if (synced) return synced;
const id = String(modelId ?? '').replace(/^openrouter\//, '');
- return MODEL_PRICES[id] ?? FALLBACK_PRICE;
+ const price = MODEL_PRICES[id] ?? FALLBACK_PRICE;
+ return price.long && promptTokens > price.long.over ? price.long : price;
}
/** USD cost of one call from the AI SDK's `usage`. */
diff --git a/server/src/lib/improve-cost.test.js b/server/src/lib/improve-cost.test.js
index 4be7ac25b..bbffc182c 100644
--- a/server/src/lib/improve-cost.test.js
+++ b/server/src/lib/improve-cost.test.js
@@ -39,6 +39,36 @@ describe('generationCostUsd', () => {
it('treats missing usage as zero tokens', () => {
expect(generationCostUsd('openai/gpt-5-mini', undefined)).toBe(0);
});
+
+ it("prices the Content Editor's Claude models: Haiku 5.5 reads, Sonnet 5.5 writes", () => {
+ // Haiku 5.5: $0.10 / $0.50 per 1M; Sonnet 5.5: $2 / $10.
+ expect(
+ generationCostUsd('anthropic/claude-haiku-5-5', { inputTokens: 7000, outputTokens: 2000 }),
+ ).toBeCloseTo(0.0017, 6);
+ expect(
+ generationCostUsd('anthropic/claude-sonnet-5-5', { inputTokens: 7000, outputTokens: 2000 }),
+ ).toBeCloseTo(0.034, 6);
+ expect(priceFor('anthropic/claude-sonnet-5-5')).not.toBe(FALLBACK_PRICE);
+ });
+
+ it("charges Haiku 5.5's long rate, $0.50 / $2.50, on a prompt past 100k tokens only", () => {
+ expect(
+ generationCostUsd('anthropic/claude-haiku-5-5', {
+ inputTokens: 100_000,
+ outputTokens: 1_000,
+ }),
+ ).toBeCloseTo(0.0105, 6);
+ expect(
+ generationCostUsd('anthropic/claude-haiku-5-5', {
+ inputTokens: 100_001,
+ outputTokens: 1_000,
+ }),
+ ).toBeCloseTo(0.0525005, 7);
+ // A model with one rate has none.
+ expect(
+ generationCostUsd('anthropic/claude-sonnet-5-5', { inputTokens: 200_000, outputTokens: 0 }),
+ ).toBe(0.4);
+ });
});
describe('priceFor with synced prices (CHAT-16 §A)', () => {
@@ -87,6 +117,8 @@ describe('the same 1% budget buys more on a cheaper model', () => {
expect(left('google/gemini-3-flash-preview')).toBe(262);
expect(left('openai/gpt-5-mini')).toBe(433);
expect(left('anthropic/claude-haiku-4-5')).toBe(146);
+ expect(left('anthropic/claude-haiku-5-5')).toBe(1464);
+ expect(left('anthropic/claude-sonnet-5-5')).toBe(73);
});
});
diff --git a/server/src/lib/structured-output.js b/server/src/lib/structured-output.js
new file mode 100644
index 000000000..63c7a7d8e
--- /dev/null
+++ b/server/src/lib/structured-output.js
@@ -0,0 +1,129 @@
+import { jsonSchema, zodSchema } from 'ai';
+
+/**
+ * generateObject's settings for a "provider/model" string (ai-provider.js
+ * resolveModel): the schema, and for Claude what its Messages API asks of a
+ * call since Claude Haiku 5.5 and Sonnet 5.5. Any other provider gets the
+ * schema as it is and its own defaults, as before.
+ *
+ * For Claude:
+ * * native structured output (output_config.format), never
+ * @ai-sdk/anthropic's "json" tool: for a model it doesn't know (3.0.58
+ * knows none past 4.6) the SDK picks the tool and forces it with
+ * tool_choice "any", which Sonnet 5.5 turns away with a 400;
+ * * the schema without the keywords output_config.format turns away with
+ * a 400 (claudeJsonSchema); the zod schema still checks the answer;
+ * * maxOutputTokens, sent: for a model it doesn't know the SDK sends
+ * 4,096, and Claude 5.5's thinking, on by default, counts toward it;
+ * * no temperature, top_p, top_k or thinking settings: the Messages API
+ * turns sampling settings away (2026-10-21), and a thinking budget or
+ * "disabled" too. None is passed here or by the calls that use this.
+ *
+ * @param {string} modelId
+ * @param {import('zod').ZodType} schema
+ * @param {{ maxOutputTokens: number }} options
+ */
+export function objectOptions(modelId, schema, { maxOutputTokens }) {
+ if (!isClaude(modelId)) return { schema };
+ return {
+ schema: claudeSchema(schema),
+ maxOutputTokens,
+ providerOptions: { anthropic: { structuredOutputMode: 'outputFormat' } },
+ };
+}
+
+/** A model the first-party Anthropic SDK runs ("anthropic/…"), not one through OpenRouter. */
+export function isClaude(modelId) {
+ return String(modelId ?? '').split('/')[0] === 'anthropic';
+}
+
+/** A zod schema as Claude's structured output takes it, still checked by zod. */
+export function claudeSchema(schema) {
+ const zod = zodSchema(schema);
+ return jsonSchema(() => claudeJsonSchema(zod.jsonSchema), { validate: zod.validate });
+}
+
+/** Keywords output_config.format turns away; a length or count is told in the description. */
+const DROPPED = new Set([
+ '$schema',
+ 'minLength',
+ 'maxLength',
+ 'pattern',
+ 'minimum',
+ 'maximum',
+ 'exclusiveMinimum',
+ 'exclusiveMaximum',
+ 'multipleOf',
+ 'maxItems',
+ 'uniqueItems',
+ 'minProperties',
+ 'maxProperties',
+ 'propertyNames',
+ 'contains',
+]);
+/** The string formats it takes. */
+const FORMATS = new Set([
+ 'date-time',
+ 'time',
+ 'date',
+ 'duration',
+ 'email',
+ 'hostname',
+ 'uri',
+ 'ipv4',
+ 'ipv6',
+ 'uuid',
+]);
+/** What a dropped keyword asked, in words the model reads in the description. */
+const SAID = {
+ minLength: (n) => `at least ${n} characters`,
+ maxLength: (n) => `at most ${n} characters`,
+ minItems: (n) => `at least ${n} items`,
+ maxItems: (n) => `at most ${n} items`,
+ minimum: (n) => `at least ${n}`,
+ maximum: (n) => `at most ${n}`,
+};
+/** zod's .int() bounds: no limit worth telling. */
+const SAFE = Number.MAX_SAFE_INTEGER;
+
+/**
+ * A JSON schema as Claude's structured output takes it: every object closed
+ * (additionalProperties false), `oneOf` as `anyOf`, and no keyword it turns
+ * away (string lengths, number ranges, item counts beyond minItems 1, a
+ * format it doesn't know, $schema). A length or count that matters is told
+ * in the description instead, so the model keeps to it: zod, checking the
+ * answer, still rejects one that doesn't. Property names are never read as
+ * keywords (a property may be called "format").
+ */
+export function claudeJsonSchema(node) {
+ if (Array.isArray(node)) return node.map(claudeJsonSchema);
+ if (!node || typeof node !== 'object') return node;
+ const out = {};
+ const said = [];
+ for (const [key, value] of Object.entries(node)) {
+ if (key === 'properties' || key === '$defs' || key === 'definitions') {
+ out[key] = Object.fromEntries(
+ Object.entries(value).map(([name, schema]) => [name, claudeJsonSchema(schema)]),
+ );
+ } else if (['items', 'anyOf', 'allOf', 'prefixItems', 'not'].includes(key)) {
+ out[key] = claudeJsonSchema(value);
+ } else if (key === 'oneOf') {
+ out.anyOf = claudeJsonSchema(value);
+ } else if (key === 'minItems') {
+ out.minItems = Math.min(value, 1);
+ if (value > 1) said.push(SAID.minItems(value));
+ } else if (key === 'format') {
+ if (FORMATS.has(value)) out.format = value;
+ } else if (DROPPED.has(key)) {
+ if (SAID[key] && Math.abs(value) < SAFE) said.push(SAID[key](value));
+ } else {
+ out[key] = value;
+ }
+ }
+ if (out.type === 'object' || out.properties) out.additionalProperties = false;
+ if (said.length) {
+ const note = `(${said.join(', ')})`;
+ out.description = out.description ? `${out.description} ${note}` : note;
+ }
+ return out;
+}
diff --git a/server/src/lib/structured-output.test.js b/server/src/lib/structured-output.test.js
new file mode 100644
index 000000000..914ced0c4
--- /dev/null
+++ b/server/src/lib/structured-output.test.js
@@ -0,0 +1,279 @@
+import { createAnthropic } from '@ai-sdk/anthropic';
+import { NoObjectGeneratedError, generateObject } from 'ai';
+import { afterEach, describe, expect, it, vi } from 'vitest';
+import { z } from 'zod';
+import { claudeJsonSchema, isClaude, objectOptions } from './structured-output.js';
+
+/**
+ * generateObject's settings for Claude 5.5 (Haiku reads, Sonnet writes, in
+ * the Content Editor): native structured output, a schema it takes, a token
+ * cap its thinking fits in, and no sampling or thinking settings. Requests
+ * go to a fake fetch: nothing reaches Anthropic.
+ */
+
+vi.mock('../config/supabase.js', () => ({ default: {} }));
+
+const { suggestionsSchema } = await import('./content-agent.js');
+const { pageSchema } = await import('./content-writer.js');
+const { rewriteSchema } = await import('./content-rewrite.js');
+const { briefSchema } = await import('./content-brief.js');
+
+afterEach(() => {
+ vi.unstubAllEnvs();
+});
+
+/** Keywords Claude's structured output turns away with a 400. */
+const REFUSED = [
+ '$schema',
+ 'minLength',
+ 'maxLength',
+ 'pattern',
+ 'minimum',
+ 'maximum',
+ 'exclusiveMinimum',
+ 'exclusiveMaximum',
+ 'multipleOf',
+ 'maxItems',
+ 'oneOf',
+];
+
+/**
+ * What in a JSON schema Claude would refuse: a keyword above, minItems over
+ * 1, an object left open; with the optional properties and unions counted
+ * (at most 24 and 16 in a request).
+ */
+function claudeCheck(schema) {
+ const problems = [];
+ let optional = 0;
+ let unions = 0;
+ const walk = (node, path) => {
+ if (Array.isArray(node)) return node.forEach((n, i) => walk(n, `${path}/${i}`));
+ if (!node || typeof node !== 'object') return;
+ for (const key of REFUSED) if (key in node) problems.push(`${path}: ${key}`);
+ if (node.minItems > 1) problems.push(`${path}: minItems ${node.minItems}`);
+ if (node.type === 'object' && node.additionalProperties !== false) {
+ problems.push(`${path}: open object`);
+ }
+ if (node.anyOf || Array.isArray(node.type)) unions += 1;
+ const required = new Set(node.required ?? []);
+ for (const [name, child] of Object.entries(node.properties ?? {})) {
+ if (!required.has(name)) optional += 1;
+ walk(child, `${path}/${name}`);
+ }
+ for (const key of ['items', 'anyOf', 'allOf', '$defs', 'definitions']) {
+ if (node[key]) walk(node[key], `${path}/${key}`);
+ }
+ };
+ walk(schema, '');
+ return { problems, optional, unions };
+}
+
+describe('objectOptions', () => {
+ const schema = z.object({ items: z.array(z.string().max(20)).min(2).max(5) });
+
+ it('leaves another provider’s call as it was: the schema alone', () => {
+ for (const id of ['google/gemini-3-flash-preview', 'openrouter/anthropic/claude-haiku-4.5']) {
+ expect(objectOptions(id, schema, { maxOutputTokens: 16_000 })).toEqual({ schema });
+ }
+ expect(isClaude('openrouter/anthropic/claude-haiku-4.5')).toBe(false);
+ expect(isClaude('anthropic/claude-sonnet-5-5')).toBe(true);
+ });
+
+ it('gives Claude native structured output, its token cap and nothing for sampling or thinking', () => {
+ const options = objectOptions('anthropic/claude-sonnet-5-5', schema, {
+ maxOutputTokens: 32_000,
+ });
+ expect(options.maxOutputTokens).toBe(32_000);
+ expect(options.providerOptions).toEqual({
+ anthropic: { structuredOutputMode: 'outputFormat' },
+ });
+ for (const key of ['temperature', 'topP', 'topK']) expect(options).not.toHaveProperty(key);
+ expect(options.providerOptions.anthropic).not.toHaveProperty('thinking');
+ expect(options.schema.jsonSchema).toEqual({
+ type: 'object',
+ properties: {
+ items: {
+ type: 'array',
+ items: { type: 'string', description: '(at most 20 characters)' },
+ minItems: 1,
+ description: '(at least 2 items, at most 5 items)',
+ },
+ },
+ required: ['items'],
+ additionalProperties: false,
+ });
+ });
+
+ it('still checks the answer against the zod schema: a limit Claude wasn’t sent holds', async () => {
+ const { schema: claude } = objectOptions('anthropic/claude-haiku-5-5', schema, {
+ maxOutputTokens: 16_000,
+ });
+ expect(await claude.validate({ items: ['a', 'b'] })).toEqual({
+ success: true,
+ value: { items: ['a', 'b'] },
+ });
+ expect((await claude.validate({ items: ['a', 'b', 'c', 'd', 'e', 'f'] })).success).toBe(false);
+ expect((await claude.validate({ items: ['a'] })).success).toBe(false);
+ });
+});
+
+describe('claudeJsonSchema', () => {
+ it('reads property names as names, never as keywords', () => {
+ expect(
+ claudeJsonSchema({
+ type: 'object',
+ properties: {
+ format: { type: 'string', format: 'email' },
+ maximum: { type: 'integer', minimum: -9007199254740991, maximum: 9007199254740991 },
+ when: { type: 'string', format: 'not-a-format' },
+ },
+ required: ['format', 'maximum', 'when'],
+ }),
+ ).toEqual({
+ type: 'object',
+ properties: {
+ format: { type: 'string', format: 'email' },
+ // zod's .int() bounds aren't a limit worth telling.
+ maximum: { type: 'integer' },
+ when: { type: 'string' },
+ },
+ required: ['format', 'maximum', 'when'],
+ additionalProperties: false,
+ });
+ });
+
+ it('turns oneOf into anyOf, and closes objects inside unions and arrays', () => {
+ expect(
+ claudeJsonSchema({
+ oneOf: [{ type: 'object', properties: { a: { type: 'string' } } }, { type: 'null' }],
+ }),
+ ).toEqual({
+ anyOf: [
+ { type: 'object', properties: { a: { type: 'string' } }, additionalProperties: false },
+ { type: 'null' },
+ ],
+ });
+ });
+});
+
+describe("the Content Editor's schemas, as Claude takes them", () => {
+ const schemas = () => ({
+ suggestionsSchema,
+ pageSchema,
+ rewriteSchema,
+ briefSchema: briefSchema(),
+ });
+
+ it('send nothing Claude refuses, within its limits on optional properties and unions', () => {
+ for (const rules of ['', 'true']) {
+ // The brief rules (2026) give the brief its version 2 schema.
+ vi.stubEnv('CONTENT_BRIEF_RULES', rules);
+ for (const [name, schema] of Object.entries(schemas())) {
+ const { schema: claude } = objectOptions('anthropic/claude-sonnet-5-5', schema, {
+ maxOutputTokens: 32_000,
+ });
+ const check = claudeCheck(claude.jsonSchema);
+ expect({ name, problems: check.problems }).toEqual({ name, problems: [] });
+ expect(check.optional).toBeLessThanOrEqual(24);
+ expect(check.unions).toBeLessThanOrEqual(16);
+ }
+ }
+ });
+
+ it('tell the model the limits they had: at most 8 suggestions, 2 to 12 sections', () => {
+ const suggestions = objectOptions('anthropic/claude-sonnet-5-5', suggestionsSchema, {
+ maxOutputTokens: 32_000,
+ }).schema.jsonSchema;
+ expect(suggestions.properties.suggestions.description).toBe('(at most 8 items)');
+ const page = objectOptions('anthropic/claude-sonnet-5-5', pageSchema, {
+ maxOutputTokens: 32_000,
+ }).schema.jsonSchema;
+ expect(page.properties.sections.description).toBe('(at least 2 items, at most 12 items)');
+ expect(page.properties.sections.minItems).toBe(1);
+ });
+});
+
+describe('a Claude 5.5 call through @ai-sdk/anthropic, as sent', () => {
+ /** A provider whose requests land here, answered with `answer` after a thinking block. */
+ function fakeClaude(answer) {
+ const sent = [];
+ const provider = createAnthropic({
+ apiKey: 'test-key',
+ // Named, so an ANTHROPIC_BASE_URL in the shell doesn't move it.
+ baseURL: 'https://api.anthropic.com/v1',
+ fetch: async (url, init) => {
+ sent.push({ url: String(url), body: JSON.parse(init.body) });
+ return new globalThis.Response(
+ JSON.stringify({
+ type: 'message',
+ id: 'msg_1',
+ model: 'claude-sonnet-5-5',
+ role: 'assistant',
+ // Thinking is on by default on Claude 5.5: its block comes first.
+ content: [
+ { type: 'thinking', thinking: '', signature: 'sig' },
+ { type: 'text', text: JSON.stringify(answer) },
+ ],
+ stop_reason: 'end_turn',
+ stop_sequence: null,
+ usage: { input_tokens: 1200, output_tokens: 900 },
+ }),
+ { status: 200, headers: { 'content-type': 'application/json' } },
+ );
+ },
+ });
+ return { provider, sent };
+ }
+
+ const suggestion = {
+ kind: 'replace',
+ passage: 1,
+ before: 'Old words',
+ after: 'New words',
+ reason: 'Answers first',
+ rules: ['answerFirst'],
+ sources: [],
+ marks: [],
+ };
+
+ it('sends output_config.format, never a forced tool, sampling or thinking settings', async () => {
+ const { provider, sent } = fakeClaude({ suggestions: [suggestion] });
+ const result = await generateObject({
+ model: provider('claude-sonnet-5-5'),
+ ...objectOptions('anthropic/claude-sonnet-5-5', suggestionsSchema, {
+ maxOutputTokens: 32_000,
+ }),
+ system: 'You are the Content Agent.',
+ prompt: 'Fix the open rules.',
+ });
+
+ expect(sent).toHaveLength(1);
+ expect(sent[0].url).toBe('https://api.anthropic.com/v1/messages');
+ const { body } = sent[0];
+ expect(body.model).toBe('claude-sonnet-5-5');
+ expect(body.max_tokens).toBe(32_000);
+ // Sonnet 5.5 refuses tool_choice "any" / "tool": the json tool isn't used.
+ expect(body).not.toHaveProperty('tools');
+ expect(body).not.toHaveProperty('tool_choice');
+ for (const key of ['temperature', 'top_p', 'top_k', 'thinking']) {
+ expect(body).not.toHaveProperty(key);
+ }
+ expect(body.output_config.format.type).toBe('json_schema');
+ expect(claudeCheck(body.output_config.format.schema).problems).toEqual([]);
+ // The thinking block is left out of the answer read.
+ expect(result.object.suggestions).toEqual([suggestion]);
+ expect(result.usage).toMatchObject({ inputTokens: 1200, outputTokens: 900 });
+ });
+
+ it('rejects an answer past a limit Claude was only told, as zod did before', async () => {
+ const { provider } = fakeClaude({ suggestions: Array(9).fill(suggestion) });
+ const call = generateObject({
+ model: provider('claude-haiku-5-5'),
+ ...objectOptions('anthropic/claude-haiku-5-5', suggestionsSchema, {
+ maxOutputTokens: 16_000,
+ }),
+ prompt: 'Fix the open rules.',
+ });
+ await expect(call).rejects.toSatisfy((err) => NoObjectGeneratedError.isInstance(err));
+ });
+});
diff --git a/server/src/scripts/eval-brief.js b/server/src/scripts/eval-brief.js
index d6fe2f5d7..6ec2c690f 100644
--- a/server/src/scripts/eval-brief.js
+++ b/server/src/scripts/eval-brief.js
@@ -6,10 +6,12 @@
* content-brief-eval.test.js to read on every test run.
*
* Run from server/, with .env holding the providers' keys for both models
- * (the brief's is IMPROVE_SUGGESTION_MODEL, else DEFAULT_SUGGESTION_MODEL,
- * else google/gemini-3-flash-preview; the judge's BRIEF_EVAL_JUDGE_MODEL,
- * else anthropic/claude-haiku-4-5) and the Supabase keys the modules load
- * with (nothing is read or written there):
+ * (the brief's is the Content Editor's read model, contentReadModelId:
+ * CONTENT_READ_MODEL, else anthropic/claude-haiku-5-5 with
+ * ANTHROPIC_API_KEY, else IMPROVE_SUGGESTION_MODEL, DEFAULT_SUGGESTION_MODEL
+ * or google/gemini-3-flash-preview; the judge's BRIEF_EVAL_JUDGE_MODEL, else
+ * anthropic/claude-haiku-4-5) and the Supabase keys the modules load with
+ * (nothing is read or written there):
* node src/scripts/eval-brief.js [count]
* count defaults to every case: two calls each, the brief and its judge.
*
@@ -30,11 +32,12 @@ import { generateObject } from 'ai';
import { CASES } from '../lib/__fixtures__/brief-eval/cases.js';
import { resolveModel } from '../lib/ai-provider.js';
import {
+ CONTENT_READ_MAX_TOKENS,
briefRulesOn,
briefSchema,
briefSystemPrompt,
buildBriefPrompt,
- contentBriefModelId,
+ contentReadModelId,
shapeBrief,
} from '../lib/content-brief.js';
import { qualityPhase } from '../lib/content-quality.js';
@@ -49,6 +52,7 @@ import {
readJudged,
summarize,
} from '../lib/content-brief-eval.js';
+import { objectOptions } from '../lib/structured-output.js';
const count = Math.max(
1,
@@ -58,7 +62,7 @@ const count = Math.max(
/** How long one call may take before the case is recorded as failed. */
const CALL_TIMEOUT_MS = 120_000;
-const modelId = contentBriefModelId();
+const modelId = contentReadModelId();
const judgeModelId = briefEvalJudgeModelId();
const model = resolveModel(modelId);
const judge = resolveModel(judgeModelId);
@@ -70,7 +74,7 @@ for (const c of CASES.slice(0, count)) {
try {
const written = await generateObject({
model,
- schema: briefSchema(),
+ ...objectOptions(modelId, briefSchema(), { maxOutputTokens: CONTENT_READ_MAX_TOKENS }),
system: briefSystemPrompt(),
prompt: buildBriefPrompt(c),
abortSignal: AbortSignal.timeout(CALL_TIMEOUT_MS),
@@ -79,7 +83,7 @@ for (const c of CASES.slice(0, count)) {
// The judge at its model's own temperature: newer models take no other.
const graded = await generateObject({
model: judge,
- schema: judgeSchema,
+ ...objectOptions(judgeModelId, judgeSchema, { maxOutputTokens: CONTENT_READ_MAX_TOKENS }),
system: JUDGE_SYSTEM_PROMPT,
prompt: buildJudgePrompt(c, brief),
abortSignal: AbortSignal.timeout(CALL_TIMEOUT_MS),
diff --git a/server/src/scripts/eval-no-fabrication.js b/server/src/scripts/eval-no-fabrication.js
index 49db12dd4..a46ff572c 100644
--- a/server/src/scripts/eval-no-fabrication.js
+++ b/server/src/scripts/eval-no-fabrication.js
@@ -8,7 +8,9 @@
* ones checked into recorded.json's `reviewed`, by draft.
*
* Run from server/, with .env holding the content model's provider key (the
- * model is IMPROVE_SUGGESTION_MODEL, else DEFAULT_SUGGESTION_MODEL, else
+ * Content Agent's writing model, contentWriteModelId: CONTENT_WRITE_MODEL,
+ * else anthropic/claude-sonnet-5-5 with ANTHROPIC_API_KEY, else
+ * IMPROVE_SUGGESTION_MODEL, DEFAULT_SUGGESTION_MODEL or
* google/gemini-3-flash-preview) and the Supabase keys the agent's modules
* load with (nothing is read or written there):
* node src/scripts/eval-no-fabrication.js [count]
@@ -33,9 +35,14 @@ import {
draftPassages,
suggestionsSchema,
} from '../lib/content-agent.js';
-import { briefRulesOn, contentBriefModelId } from '../lib/content-brief.js';
+import {
+ CONTENT_WRITE_MAX_TOKENS,
+ briefRulesOn,
+ contentWriteModelId,
+} from '../lib/content-brief.js';
import { inputSources } from '../lib/content-inputs.js';
import { numberSources, sourcesPrompt } from '../lib/content-sources.js';
+import { objectOptions } from '../lib/structured-output.js';
import {
writeSystemPrompt,
briefText,
@@ -55,7 +62,8 @@ const RULES = [
{ key: 'example', name: 'A real customer example', detail: 'missing' },
];
-const modelId = contentBriefModelId();
+const modelId = contentWriteModelId();
+const write = { maxOutputTokens: CONTENT_WRITE_MAX_TOKENS };
const model = resolveModel(modelId);
const recorded = {};
const started = Date.now();
@@ -85,7 +93,7 @@ for (const c of CASES.slice(0, count)) {
const [fix, page] = await Promise.all([
generateObject({
model,
- schema: suggestionsSchema,
+ ...objectOptions(modelId, suggestionsSchema, write),
system: SYSTEM_PROMPT,
prompt: buildFixPrompt({
brand,
@@ -98,7 +106,7 @@ for (const c of CASES.slice(0, count)) {
}),
generateObject({
model,
- schema: pageSchema,
+ ...objectOptions(modelId, pageSchema, write),
system: writeSystemPrompt(),
prompt: buildWritePrompt({ brand, draft, numbered, brief: briefText(null, null), input }),
}),