-
Notifications
You must be signed in to change notification settings - Fork 89
Description
Description
Context
I've been exploring the Prompt API for use cases that primarily involve single-turn conversations—quick, independent prompts where conversation history isn't needed. Examples include:
- Classification tasks ("Is this text spam?")
- One-off transformations ("Summarize this paragraph")
- Extracting structured data from a snippet of text
Current Approach
To achieve stateless single-turn behavior today, the recommended pattern seems to be:
const templateSession = await LanguageModel.create({
initialPrompts: [{ role: "system", content: "You are a helpful assistant." }]
});
async function singleTurnPrompt(userMessage) {
const freshSession = await templateSession.clone();
const result = await freshSession.prompt(userMessage);
freshSession.destroy();
return result;
}Alternatively, reusing the same session causes conversation history to accumulate, which can influence later prompts and eventually triggers quotaoverflow.
The Problem
The clone-prompt-destroy cycle introduces overhead for every single-turn prompt:
clone()— Copies session state (async, takes time)prompt()— Runs the actual inferencedestroy()— Cleans up the cloned session
For high-frequency single-turn use cases, this overhead adds up and feels unnecessary.
Proposal
I'd like to suggest considering one of the following (or a combination):
Option A: Stateless session mode
A session option that automatically discards conversation history after each prompt:
const session = await LanguageModel.create({ stateless: true });
// Each prompt is independent—no history retained
const result1 = await session.prompt("Is this spam? 'You won a prize!'");
const result2 = await session.prompt("Summarize: 'The quick brown fox...'");Option B: Static single-turn method
A convenience method on LanguageModel for one-off prompts without explicit session management:
const result = await LanguageModel.prompt("What is 2 + 2?");
// With options
const result2 = await LanguageModel.prompt("Classify this sentiment", {
systemPrompt: "You are a sentiment classifier. Respond with: positive, negative, or neutral.",
temperature: 0.2
});Option C: Browser-managed session pool
The browser maintains a pool of pre-warmed sessions internally. Single-turn calls would draw from this pool, avoiding the setup cost:
const result = await LanguageModel.promptOnce("Quick question here");Benefits
- Performance — Eliminates clone/destroy overhead for single-turn use cases
- Developer experience — Simpler API for common use cases
- Resource efficiency — Browser could optimize session reuse internally better than userland code