vllm.renderers.prefix_token_cache
¶
Token ids of recent long prompts, reused by the next turn of a conversation.
A multi-turn (agentic) chat request carries the whole conversation, and the next turn's rendered prompt starts with the previous turn's text. An HF fast tokenizer splits its added tokens (the chat template's special tokens) out of the text before it normalizes and pre-tokenizes the pieces between them, so the ids of a text equal the ids of its pieces cut right after any added token, concatenated. This cache keeps, for each recent long prompt, its ids and a rolling digest of the text at every such cut; a new prompt reuses the ids up to its deepest cut that a cached prompt shares and encodes only the rest, piece by piece.
Measured on 600 InferenceX AgentX requests (MiniMax-M3.1, prompts p50 479K characters / 157K tokens): encode p50 152 -> 9 ms, p90 525 -> 103 ms, ids equal on every request; 98% of the characters are reused at the median.
Used only when every added token matches the raw text (no lstrip / rstrip
/ single_word / normalized), for prompts of at least MIN_CHARS encoded
without special tokens and without offsets. The first VERIFY_FIRST prompts it
serves are also encoded whole and compared; a mismatch turns it off for the
process.
Classes:
PrefixTokenCache
¶
Methods:
-
encode–The ids of
textencoded without special tokens. -
maybe_create–A cache for an HF fast tokenizer whose added tokens match the raw
Source code in vllm/renderers/prefix_token_cache.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
encode(text)
¶
The ids of text encoded without special tokens.
Source code in vllm/renderers/prefix_token_cache.py
maybe_create(tokenizer)
classmethod
¶
A cache for an HF fast tokenizer whose added tokens match the raw text, else None.