Skip to main content
The OpenAIChatCompletionsTokenClient class extends OpenAIChatCompletionsClient to use vLLM’s custom /v1/chat/completions/tokens endpoint for token-level prompt stitching (TITO - token-in, token-out) instead of message-level inference (MITO - message-in, token-out).

Overview

This client optimizes multi-turn conversations by reusing tokenized prompts from previous turns rather than re-tokenizing the entire conversation history on each turn. It:
  • Detects message-level prefix matches in the conversation trajectory
  • Reuses token IDs from previous turns when possible
  • Handles chat template suffix tokens correctly
  • Falls back to standard message-based inference for the first turn or when multimodal content is present
  • Automatically manages token stitching across truncated turns
This client requires a vLLM server with the custom /v1/chat/completions/tokens and /tokenize endpoints. It is designed for inference optimization and will fall back to standard behavior when necessary.

Type Aliases

Inherits from OpenAIChatCompletionsClient:
Additional response type:

Class Definition

Inherits all generic type parameters from OpenAIChatCompletionsClient:
  • ClientT: AsyncOpenAI
  • MessagesT: OpenAIChatMessages
  • ResponseT: OpenAIChatResponse
  • ToolT: OpenAITool

Constructor

AsyncOpenAI | ClientConfig
required
Either a pre-configured AsyncOpenAI client or a ClientConfig to create one. The base URL should point to a vLLM server.

Example

Properties

token_client

Returns an AsyncOpenAI client with the base URL stripped of /v1 suffix for accessing vLLM’s /tokenize endpoint. Returns: AsyncOpenAI instance configured for tokenization endpoints.

Methods

get_native_response

Calls the vLLM Chat Completions API, using token-level inference when possible.
OpenAIChatMessages
required
List of OpenAI message parameters.
str
required
Model identifier hosted on vLLM.
SamplingArgs
required
Sampling parameters. max_tokens is automatically renamed to max_completion_tokens. logprobs is automatically set to True and return_token_ids=True is added to extra_body.
list[OpenAITool] | None
default:"None"
Optional list of tools in OpenAI format.
dict
Must include state (type State) for accessing trajectory and managing cached tokens.
Returns: OpenAI ChatCompletion object. Behavior:
  • First turn (len(state["trajectory"]) == 0): Uses standard /chat/completions endpoint (MITO)
  • Multimodal content present: Falls back to standard endpoint because vLLM’s /tokenize doesn’t run multimodal processor
  • Subsequent text-only turns: Uses /chat/completions/tokens endpoint (TITO) with token stitching
  • No prefix match found: Falls back to standard endpoint

get_prompt_ids

Builds prompt token IDs by finding the longest message-level prefix match in the trajectory and stitching with new tokens.
State
required
Current rollout state containing trajectory history.
OpenAIChatMessages
required
Current prompt messages to convert to token IDs.
list[OpenAITool] | None
required
Tools in OpenAI format (affects tokenization).
Returns: List of token IDs representing the full prompt, or None if no prefix match found. Algorithm:
  1. Scans trajectory backwards to find the step whose messages form the longest prefix of prompt_messages
  2. Extracts token IDs from that step (prompt_ids + completion_ids)
  3. Computes and appends chat template suffix tokens (e.g., EOM tokens)
  4. Tokenizes the full prompt to derive environment response tokens
  5. Returns prev_turn_ids + suffix_ids + env_response_ids

tokenize

Tokenizes messages or text using the vLLM /tokenize API.
str | OpenAIChatMessages
required
Either a plain text string or a list of OpenAI message parameters.
list[OpenAITool] | None
required
Optional tools (affects tokenization of messages).
str
required
Model identifier for tokenization.
dict
default:"{}"
Additional parameters for tokenization (e.g., add_generation_prompt).
Returns: List of token IDs.

Usage Example

Multi-Turn Example

State Keys

The client manages these keys in state:
list[int]
Cached chat template suffix tokens computed once per rollout. Used to correctly handle message delimiter tokens across turns.

TITO vs MITO

Message-In Token-Out (MITO)

Standard behavior:
  • Sends full message history on each turn
  • Server re-tokenizes everything
  • Used for: first turn, multimodal content

Token-In Token-Out (TITO)

Optimized behavior:
  • Sends token IDs directly to skip re-tokenization
  • Reuses cached tokens from previous turns
  • Stitches new tokens for environment responses
  • Used for: subsequent text-only turns
Performance benefit: TITO eliminates redundant tokenization overhead in multi-turn conversations, especially valuable for:
  • Long conversation histories
  • Models with complex chat templates
  • High-throughput inference scenarios

Multimodal Content Handling

The client automatically detects multimodal content (images, audio) and falls back to MITO:
Reason: vLLM ≤0.16’s /tokenize endpoint doesn’t run the multimodal processor, so image placeholders stay collapsed (1 token instead of N) and token-stitching produces broken prompts.

Chat Template Suffix Tokens

The client handles chat template suffix tokens (e.g., EOM tokens, newlines) correctly:
  1. Computes suffix tokens once using dummy messages
  2. Caches them in state["_cached_suffix_ids"]
  3. For each turn, finds the largest overlap between previous turn tokens and suffix tokens
  4. Appends non-overlapping suffix tokens to handle truncated turns
This ensures that token stitching respects the chat template format even when turns are truncated mid-message.

Fallback Conditions

The client falls back to standard MITO when:
  1. First turn: len(state["trajectory"]) == 0
  2. Multimodal content: Current or any previous turn contains images/audio
  3. No prefix match: get_prompt_ids() returns None

Error Handling

Inherits error handling from OpenAIChatCompletionsClient:
  • Context length errors → OverlongPromptError
  • Empty responses → EmptyModelResponseError
  • Invalid responses → InvalidModelResponseError
  • Authentication errors → Re-raised from provider

See Also