---
title: "Character Prefix Conditioning"
description: "The post poses a problem in code completion: generating a language-model completion that begins with the characters already typed by the user. Because modern models process token sequences rather than characters, naively tokenizing the input can produce the wrong result when the cursor falls inside a token. It introduces character prefix conditioning, defined as sampling tokens from an autoregressive distribution while constraining their decoded character string to start with the given prefix. The challenge is to construct an efficient autoregressive algorithm for this conditional distribution while minimizing calls to the original language model; the post requests either an algorithm description or an implementation."
---

# Character Prefix Conditioning

[Cursor](https://yomu.fyi/company/cursor) · Jacob Jackson · Jan 6, 2025

**Type:** Problem & solution

## Summary

The post poses a problem in code completion: generating a language-model completion that begins with the characters already typed by the user. Because modern models process token sequences rather than characters, naively tokenizing the input can produce the wrong result when the cursor falls inside a token. It introduces character prefix conditioning, defined as sampling tokens from an autoregressive distribution while constraining their decoded character string to start with the given prefix. The challenge is to construct an efficient autoregressive algorithm for this conditional distribution while minimizing calls to the original language model; the post requests either an algorithm description or an implementation.

## Context

Code completion should begin with the user's typed characters, but language models operate on token sequences. Naive tokenization gives incorrect results when the cursor does not align with a token boundary.

## Takeaways

- Character prefix conditioning samples a token sequence conditional on a prefix of characters rather than a prefix of tokens.
- The sampled token sequence must decode to a character string beginning with the user's supplied prefix.
- The requested algorithm should sample autoregressively while minimizing calls to the original language model.

**Tags:** [LLMs](https://yomu.fyi/topic/llm)

- Source: [Cursor](https://cursor.com/blog/cpc)
- Source URL: https://cursor.com/blog/cpc
- Ingested by Yomu: 2026-09-02T11:45:59.090Z

[Read original post](https://cursor.com/blog/cpc)
