Skip to the content.

Advanced

Two API layers

The high-level calls are not a weaker API: they accept the same options (samplingMode, toolCallingMode, reasoningLevel, includeSchemaInPrompt, metadata) and return the raw pieces too (rawContent, transcriptEntries, usage).

Budgeting the context window

const { contextSize } = await AppleFoundationModels.getModelInfo();            // e.g. 4096
const used = await AppleFoundationModels.tokenCount({                            // iOS 26.4+
  instructions, tools, schema, transcript: await session.getTranscript(),
});
if (used > contextSize * 0.8) {
  const summary = await session.respond('Summarize our conversation in 5 bullet points.');
  session.dispose();
  session = await createSession({ instructions: `${instructions}\n\nConversation so far:\n${summary.text}` });
}

On iOS 27, CONTEXT_SIZE_EXCEEDED errors include details.contextSize and details.tokenCount.

Reasoning (iOS 27)

Models with the reasoning capability accept reasoningLevel: 'light' | 'moderate' | 'deep' | { custom }. Reasoning tokens are reported in usage.reasoningTokens and reasoning entries appear in the transcript. Deeper reasoning costs latency and tokens; use it per request or per profile.

Deterministic-ish output

await session.respond(prompt, { samplingMode: { type: 'greedy' } });
await session.respond(prompt, { samplingMode: { type: 'topK', k: 20, seed: 42 } });

Seeds make sampling repeatable for the same model version. Apple updates the model with OS releases, so expect changes across iOS versions.

Performance

Concurrency model

Refusals and guardrails

GUARDRAIL_VIOLATION means Apple’s safety system blocked input or output; REFUSAL means the model declined. For legitimate “transform the user’s own text” features (summarize, rewrite), consider systemModel({ guardrails: 'permissiveContentTransformations' }).

Feedback to Apple

session.createFeedbackAttachment({ sentiment, issues, desiredResponse }) returns data for Apple’s Feedback Assistant (includes the transcript, including rolled-back entries). Only submit with the user’s consent.

Testing

Version awareness

getDiagnostics().build shows which SDK the app was compiled with (ios27SDK), and features shows what is usable at runtime. An app built with Xcode 26 running on iOS 27 won’t have iOS 27 features (SDK_VERSION).