v148 · AI · Prompt API

Conversation History

A multi-turn chat backed by Chrome's on-device Gemini Nano via the Prompt API. Watch the context window fill as the conversation grows — when it nears the limit the oldest turns are pruned to keep the session alive, with a visible history sidebar.

Checking Prompt API availability…
Start chatting — history is maintained across turns
Auto-prune when context > Keep latest

History management pattern

const session = await LanguageModel.create({
  systemPrompt: 'You are a helpful assistant.',
  // Pass previous turns on session creation to restore history
  initialPrompts: conversationHistory.map(turn => ({
    role: turn.role,           // 'user' | 'assistant'
    content: turn.content,
  })),
});

// Check token budget before sending
const remaining = session.tokensLeft;
const max = session.maxTokens;
if (remaining / max < 0.2) {
  // Prune oldest turns and recreate session
  conversationHistory = conversationHistory.slice(-6);
  await session.destroy();
  session = await createSession(conversationHistory);
}

const response = await session.prompt(userMessage);

references

implementation reference

Need the exact API surface, compatibility boundaries, errors, lifecycle, and source links? Read the matching gendn reference ↗