Crux
GuidesPrompts

Execution

Run the same prompt with Vercel AI SDK, OpenAI, Google GenAI, Anthropic, or agent frameworks.

All adapters accept the same prompt() definitions: write once, run on any SDK.

generate.ts
import { generate, stream } from '@use-crux/ai'
import { openai } from '@ai-sdk/openai'

// Structured output
const result = await generate(editDraft, {
  model: openai('gpt-4o'),
  input: { instruction: 'Fix the intro' },
})
result.object // typed as z.infer<typeof OutputSchema>

// Text output
const text = await generate(greet, {
  model: openai('gpt-4o'),
  input: { name: 'Henri' },
})
text.text // string

// Streaming
const result = await stream(editDraft, {
  model: openai('gpt-4o'),
  input: { instruction: 'Fix the intro' },
})
for await (const partial of result.partialOutputStream) { ... }

Use tool() from @use-crux/ai for AI SDK tools. Portable loop controls such as maxSteps() and hasToolCall() come from @use-crux/core.

generate.ts
import { createOpenAI } from "@use-crux/openai";
import OpenAI from "openai";

const adapter = createOpenAI(new OpenAI({ apiKey: "..." }));

// Structured output → chat.completions.parse
const result = await adapter.generate(editDraft, {
  model: "gpt-4o",
  input: { instruction: "Fix the intro" },
});
result.choices[0].message.parsed; // typed

// Text output → chat.completions.create
const text = await adapter.generate(greet, {
  model: "gpt-4o-mini",
  input: { name: "Henri" },
});

// Streaming
const stream = await adapter.stream(greet, {
  model: "gpt-4o",
  input: { name: "Henri" },
});

Accepts OpenAI-native options: tools, tool_choice, parallel_tool_calls, and all OpenAISettings.

generate.ts
import { createGoogle } from "@use-crux/google";
import { GoogleGenAI } from "@google/genai";

const adapter = createGoogle(new GoogleGenAI({ apiKey: "..." }));

// Structured output → generateContent with JSON schema
const result = await adapter.generate(editDraft, {
  model: "gemini-2.5-flash",
  input: { instruction: "Fix the intro" },
});
result.object; // parsed + validated with Zod

// Text output
const text = await adapter.generate(greet, {
  model: "gemini-2.0-flash",
  input: { name: "Henri" },
});

// Streaming
const stream = await adapter.stream(greet, {
  model: "gemini-2.5-flash",
  input: { name: "Henri" },
});

Accepts Google-native options: tools, temperature, maxOutputTokens, topP, topK, plus extra.cachedContent.skip and extra.cachedContent.ttlSeconds for per-call CachedContent control.

The Google adapter validates structured output with Zod after generation. If the model returns invalid JSON, you'll get a Zod error, not a Google SDK error.

generate.ts
import { createAnthropic } from "@use-crux/anthropic";
import Anthropic from "@anthropic-ai/sdk";

const adapter = createAnthropic(new Anthropic({ apiKey: "..." }));

// Structured output → messages.parse
const result = await adapter.generate(editDraft, {
  model: "claude-sonnet-4-5-20250929",
  input: { instruction: "Fix the intro" },
});
result.parsed_output; // typed

// Text output → messages.create
const text = await adapter.generate(greet, {
  model: "claude-haiku-4-5-20251001",
  input: { name: "Henri" },
});

// Streaming
const stream = await adapter.stream(greet, {
  model: "claude-sonnet-4-5-20250929",
  input: { name: "Henri" },
});

Accepts Anthropic-native options: tools, tool_choice, thinking, metadata.

Anthropic requires max_tokens. If not set in your prompt's settings, the adapter defaults to 4096.

Agent frameworks

For AI SDK-based agent frameworks that manage their own model calls, @use-crux/ai/agent returns composed instructions and a wrapped model instead of executing directly:

agent.ts
import { resolve } from "@use-crux/ai/agent";

const { instructions, model } = await resolve(chatAgent, {
  model: languageModel,
  input: { mode },
});

// Pass to any AI SDK-compatible agent framework
const agent = createAgent({
  model,
  instructions,
  tools,
});

See Framework Guides for specific integration examples with Convex Agent and other frameworks.

Same prompt, multiple SDKs

The same prompt works across all adapters without modification:

multi-sdk.ts
import { generate } from "@use-crux/ai";
import { createOpenAI } from "@use-crux/openai";
import { createGoogle } from "@use-crux/google";
import { createAnthropic } from "@use-crux/anthropic";

// Vercel AI SDK
await generate(sentiment, { model: openai("gpt-4o"), input });

// OpenAI SDK
const oai = createOpenAI(new OpenAI());
await oai.generate(sentiment, { model: "gpt-4o", input });

// Google GenAI
const google = createGoogle(new GoogleGenAI({ apiKey: "..." }));
await google.generate(sentiment, { model: "gemini-2.5-flash", input });

// Anthropic SDK
const anthropic = createAnthropic(new Anthropic({ apiKey: "..." }));
await anthropic.generate(sentiment, {
  model: "claude-sonnet-4-5-20250929",
  input,
});

Integrate Existing SDK Code

When migrating existing SDK messages or supplying a lightweight GenerateObjectFn or GenerateTextFn to a framework-neutral Core utility, use the focused SDK Bridges guide. Normal Prompt execution should use the complete adapter APIs shown above.

Next steps

On this page