arXiv:2610.28393cs.CLOct 2026

Manuscript Offprint

arXiv:2610.28393

When Copying Is Hard: Copy-Constrained Decoding for Exact Span Reproduction

Jinghui Zhang, Lang Gao, Zongfang Liu, Ruihong Zeng, Zirui Song, Rui Yan, Kentaro Inui, Xiuying Chen

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original. This text is shared under the same licence.

Abstract

Precisely reproducing a target span verbatim within a long context is a fundamental capability test for LLMs: even when the target string is present in context, models often still drift within the span or fail to stop correctly, revealing limitations in source grounding and boundary control. This capability has broad practical relevance. For instance, code generation often requires embedding sensitive strings such as API keys exactly as they appear in context. In this work, we construct a benchmark to test exact span reproduction ability across contracts, scientific literature, code, and random sequences, and find that current models still make surprisingly frequent errors. To address this, we propose CopyGen, a lightweight copy-constrained plugin for frozen LLMs that turns copying from a byproduct of next-token prediction into a controlled process. Our key idea is to decompose copying into three distinct decisions: when to copy, how far to copy, and when to stop. Concretely, CopyGen first determines whether to enter copy mode, then predicts a tentative copy length, and finally verifies the generated span token by token. During verification, we adaptively adjust the logits of copy continuation tokens to incentivize or suppress continuation, and emit only the longest valid prefix. Across three LLM backbones and four benchmark tasks, CopyGen improves average exact match by 35% relative without model finetuning, and integrates seamlessly with SFT to further improve it by 4%, while substantially accelerating generation. These results suggest that reliable exact copying is not something LLMs obtain for free, but a decoding-time control problem that requires explicit modeling. Code is available at https://github.com/mbzuai-nlp/CopyGen.

1 Introduction

Precisely reproducing long spans of text verbatim, referred to as instruction-conditioned long-span exact copying, can be used to evaluate large language models' ability in exact copying and controlled generation. Although it appears simple, this task places nontrivial demands on models' capabilities in sequence alignment, boundary identification, and fine-grained control over generation. At the same time, this capability has important practical value. In software engineering workflows, models must preserve API keys, identifiers, or configuration strings exactly as they appear in context [11]. In legal processing, they must reproduce clauses verbatim rather than paraphrase them [26]. In such settings, a single incorrect character can break execution or alter the meaning of a legally sensitive passage. Furthermore, when the desired span is already present in the input, regenerating it token by token is both error-prone and computationally unnecessary, making exact copying not only a practically important capability but also an opportunity for improved efficiency.

∗^{*}Corresponding author.

40th Conference on Neural Information Processing Systems (NeurIPS 2026).

Despite its practical importance, long-span exact copying has not been studied as a capability in its own right. Existing work on copying falls into two lines, neither of which addresses this problem directly. Architectural copy mechanisms such as CopyNet [8] and pointer-generator networks [25] help models reuse source tokens to improve generation quality, but do not target character-level exactness. Efficiency-oriented methods such as speculative decoding [13], LLMA [28], and CopySpec [6] accelerate inference through multi-token proposal or reuse, with correctness verified against the base model's own predictions rather than strengthened by the copy process itself.

To make this problem concrete, we construct a benchmark for instruction-conditioned exact span reproduction that focuses on preserving the required source text verbatim during generation, rather than broader questions of semantic faithfulness. Specifically, the benchmark covers three real-world tasks, namely legal contracts, scientific literature, and code contexts, where even minor deviations are unacceptable. We further include a synthetic random-sequence task to stress-test copying performance under extreme conditions. Using this benchmark, we evaluate a range of LLMs and find that standard decoding fails consistently across model families and sizes. The errors mainly take two characteristic forms. Internal token mismatches arise when the model drifts mid-span, substituting a plausible-but-wrong token for the correct one. Boundary overruns occur when the model continues generating along the source span even after the target span should have ended. These failures suggest that exact copying requires stronger control than standard next-token prediction provides, calling for an explicit copying mechanism that can maintain source alignment and enforce the correct stopping boundary.

To tackle this issue, we propose CopyGen, a lightweight copy-constrained plugin for frozen LLMs. The key idea is to treat exact copying as a controlled decoding problem rather than a byproduct of next-token prediction. CopyGen decomposes exact copying into three coupled decisions: when to copy, how far to tentatively copy, and when to stop. Concretely, at each decoding step, CopyGen measures how strongly the current output prefix is anchored to the source. Once sufficient grounding is detected, the plugin proposes a tentative copy span and replays it token by token against the frozen model. Our analysis reveals tokens that should be copied are predicted with a large logits margin over competing alternatives, while errors, whether mid-span drifts or boundary overruns, concentrate precisely where that margin narrows. During verification, we utilize a learned bias to calibrate the margin at each replayed position, reinforcing source-following where confidence is high and suppressing it where the boundary is near, emitting only the longest prefix that clears the threshold.

Across three LLM backbones and four benchmark tasks, CopyGen delivers substantial improvements in both copy accuracy and decoding efficiency. When applied to frozen models, it improves average exact match by approximately 35% relative. When combined with supervised fine-tuning (SFT), CopyGen continues to provide gains, further increasing average exact match by about 4% relative while also significantly improving decoding throughput. These results suggest that reliable exact copying is not an emergent byproduct of LLM training, but rather a capability that requires explicit control at decoding time along with dedicated copying mechanisms.

In summary, our contributions are threefold: (1) We formulate instruction-conditioned long-span exact copying as a distinct evaluation problem and introduce a dedicated benchmark for studying it. (2) We propose CopyGen, a lightweight copy-constrained plugin for frozen LLMs that augments frozen LLMs with explicit copy control during decoding. (3) We demonstrate that CopyGen substantially improves exact-copy reliability and decoding throughput across model families and scales, and remains effective when combined with supervised finetuning.

References (28)