Skip to content

Lightweight single-turn prompting mode #186

@Nithanaroy

Description

@Nithanaroy

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:

  1. clone() — Copies session state (async, takes time)
  2. prompt() — Runs the actual inference
  3. destroy() — 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions