InferenceAI

Enter at least two characters to search.

Start free

Reusable Prompt Prefix

mediumInference20 min25 XP

A prefix cache may reuse KV states only until the first token mismatch. Implement shared_prefix_length(cached_tokens, request_tokens) and return the number of equal tokens from the beginning of both lists. Token IDs are integers. Similar tokens later in the prompt do not count after a mismatch.

Examples

EXAMPLE WALKTHROUGHfirst mismatch

A guided view of the published example, not a trace of your code.

INPUT[[11,12,13,14],[11,12,99,14]]
TASKshared_prefix_length()
EXPECTED OUTPUT2

Start with the input values shown in this example.

Example 1 of 2

Input: cached_tokens = [11,12,13,14], request_tokens = [11,12,99,14]

Output: 2

Explanation: first mismatch

Input: cached_tokens = [7,8], request_tokens = [7,8,9]

Output: 2

Explanation: cached prefix

Hint 1

Stop as soon as the token IDs differ.

Topics

PythonPrefix CachingLLM Serving