A Practical Guide to Reproducing Paper Code with LLM Agents

    This article explains why code generation alone does not guarantee successful paper reproduction, and how to build a reliable LLM agent workflow around it. Based on hands-on experiments, it covers the typical failure modes of small-parameter models, how to split work across model tiers by task stage, how to add observability and guardrails to the execution chain, and how to control cost through prompt caching. It closes with a practical checklist for turning reproduction tasks into a repeatable pipeline.

    1. Why “Can Generate Code” Is Not “Can Complete Reproduction”

    When people connect a large language model to an AI coding tool to reproduce the experimental code of a paper, they often assume that “the model can write code” means “the model can deliver the task.” Engineering experience shows the opposite: code generation is a single-point capability, while paper reproduction is a long-chain task spanning many stages. A mistake in any stage can make the final result unusable.

    A typical task includes the following stages:

    1. Document parsing: extracting body text, formulas, figure and table captions, and appendices from papers with complex layouts;
    2. Requirement structuring: turning natural-language descriptions into dependency lists, data format conventions, and training workflows;
    3. Environment setup: handling interpreter versions, accelerator library and driver matching, and proxy and mirror configuration;
    4. Data preprocessing: implementing splitting, normalization, augmentation, and index construction as described in the paper;
    5. Training and validation: running a small-scale smoke test first, then aligning metrics;
    6. Result packaging: producing reproducible scripts, random seeds, and run records.

    This chain is characterized by “strong dependencies and weak feedback”: errors in intermediate artifacts often do not surface immediately but appear several rounds later, making backtracking extremely costly. So whether an agent is usable cannot be judged by how elegant its snippets look, but by its stability, sense of boundaries, and self-correction ability in long tasks.

    2. Observed Failure Modes of Small-Parameter Models

    In a controlled experiment, the task was set to “parse the paper file, set up a runnable environment in the specified repository, and complete the code reproduction,” with permissions opened to a relatively high level so that the model’s real behavior without manual fallback could be observed. With a lightweight model in the twenty-billion-plus parameter range, three typical problems appeared.

    The first is ineffective exploration. Before starting real work, the model traversed many directories unrelated to the task and repeatedly inspected other projects’ file structures, which consumed context budget and lengthened the time to first response. The root cause is a lack of task boundary constraints: the model mistook “exploring the environment” for “scanning the entire disk.”

    The second is workspace pollution. The model copied external materials in bulk into the local working directory instead of using the agreed temporary space, creating messy directory levels and mixing temporary files with source code, which significantly raised the cost of later cleanup and version management.

    The third is command-level defects. When facing poor small-file performance on a network file system, the model chose a workaround of “copy to a temporary directory first, then move back.” The idea itself was reasonable, but the path wildcard scope in the copy command was wrong, pulling in content from the parent directory and turning a local problem into global pollution.

    Tracing the execution chain confirms that the failures did not come from a single missing piece of knowledge, but from imprecise understanding of path semantics and the absence of pre-operation validation and post-operation checks. Such defects may be hidden in short tasks but will almost certainly surface in long ones.

    3. Capability Tiering: Dividing Work Across Model Sizes

    Rerunning the same task with a larger reasoning-oriented model allowed it to start normally and complete the full workflow, taking about twenty minutes in total, with a calling cost on the order of a few dollars. This comparison shows that capability tiering objectively exists in AI coding scenarios. The reasonable approach is not to expect one model to do everything, but to divide work by task stage.

    • Planning and decomposition: assign to a model with stronger reasoning ability to produce a clear step list and acceptance criteria;
    • Mechanical execution: lightweight models can handle low-risk actions such as format rewriting and comment completion;
    • High-risk operations: bulk copying, deletion, and environment changes should be executed by a strong model or forced through manual confirmation;
    • Validation: regardless of the model used, independent check scripts must be introduced rather than relying on the model’s self-report.

    It is worth stressing that parameter size is only an indirect indicator; what really determines success or failure is instruction following, tool-calling discipline, and error recovery ability. Evaluation should include test cases with “trap paths,” “extra-long context,” and “rollback required,” rather than only routine Q&A.

    4. Observability: Making Every Agent Step Reviewable

    To turn such problems from guesswork into something locatable, the key is to build observability into the execution chain. At minimum, record four kinds of information:

    1. step_id | timestamp | tool | args_hash | cwd | exit_code | diff_summary
    • Tool calls and argument summaries: locate wildcard misuse and wrong target paths;
    • Working directory snapshots: determine whether out-of-bounds writes occurred;
    • Change diff summaries: quantify the impact scope of each operation on the repository;
    • Failure and retry records: identify whether the model is stuck in a loop.

    With these records, a subjective judgment like “it executed poorly” becomes an objective conclusion: at which step, on which path, what command was run, and how large a change was produced. Agents should also have hard guardrails, such as a writable-directory whitelist, a ban on recursive wildcards in the repository root, and mandatory second confirmation for bulk operations.

    5. Cost Control: Cache Hits and Calling Strategy

    Cost is a real constraint on whether a long task can “run all night.” Besides choosing a model of appropriate size, the caching mechanism in the billing structure matters greatly. A common industry practice is to charge cache-miss input at the base rate while offering a steep discount on cache-hit portions, with the two differing by up to two orders of magnitude, which brings the marginal cost of repeated context close to zero.

    This leads to clear engineering implications:

    1. Stable prefix: put the system prompt, specification notes, and directory conventions at the very front of the context and keep them byte-level stable;
    2. Reduce jitter: avoid repeatedly rewriting the same instructions during a long task, which invalidates the cache;
    3. Segmented submission: split a large task into independently acceptable subtasks to improve reuse rate;
    4. Budget guardrails: set per-task and per-day cost caps, and pause with a summary once exceeded.

    In practice, with a high cache hit rate, the cost of running continuously overnight can be kept very low, making “batch-running experiments at night” a workable approach.

    6. A Practical Checklist: Turning Reproduction into a Repeatable Pipeline

    Overall, a robust agent reproduction workflow can be fixed into the following checklist:

    • Input layer: parse paper files uniformly, output a structured intermediate format, and preserve page and section anchors;
    • Constraint layer: specify the working directory, dependency versions, proxy configuration, and prohibited operations;
    • Execution layer: run a small smoke test first, then the full run, with each step producing verifiable artifacts;
    • Validation layer: use independent scripts to check directory cleanliness, dependency completeness, and metric deviation;
    • Review layer: aggregate the call chain, elapsed time, and cost into comparable experiment records.
    1. # Example: path safety check before an operation
    2. def safe_target(base, target):
    3. base, target = realpath(base), realpath(target)
    4. if not target.startswith(base + os.sep):
    5. raise ValueError("Out-of-bounds write blocked")
    6. return target

    7. Conclusion

    Reproducing paper code is an intensive test of an agent’s overall capability. Practice shows that lightweight models tend to exhibit ineffective exploration, directory pollution, and command defects in long-chain tasks, while models with stronger reasoning and instruction-following ability, combined with solid guardrails, can deliver within a controllable cost. For developers, the real competitive edge lies not in chasing a single model, but in building an engineering system with capability tiering, observability, rollback, and cost control. When deploying in the cloud, this system can be consolidated into reusable team infrastructure by combining general capabilities such as container platforms, object storage, logging services, and monitoring alerts.