R2Code: Teaching an AI to See Which Code Implements Which Requirement, With 7.4 Percent Better Links and 41.7 Percent Fewer Tokens
Every software feature starts as a written requirement and ends as lines of code, but the invisible thread between them is usually lost. R2Code is an AI framework that rebuilds that thread by reading a requirement, breaking it into intent, actions, conditions, and outputs, then finding the code that truly implements each part. Across five real world projects in healthcare, tourism, sensors, and public services, it found more correct links than strong keyword and AI baselines while using up to 41.7 percent fewer tokens, a major cost saving at scale.
Every software feature starts as a written requirement and ends as lines of code, but the invisible thread between them is usually lost. R2Code is an AI framework that rebuilds that thread by reading a requirement, breaking it into intent, actions, conditions, and outputs, then finding the code that truly implements each part. Across five real world projects in healthcare, tourism, sensors, and public services, it found more correct links than strong keyword and AI baselines while using up to 41.7 percent fewer tokens, a major cost saving at scale.
Why it matters
Software teams live with a quiet maintenance tax: when a requirement changes, nobody is quite sure which parts of the code must change with it. Traceability links spell out that mapping, and they power change impact analysis, code comprehension, and safer refactoring. Yet in real evolving repositories these links are frequently missing, outdated, or inconsistent, and keeping them current is a slow manual job that can consume a substantial share of development effort.
Classic tools tried to rebuild these links by matching words. That works for obvious phrasing but fails whenever a requirement is written as a business goal and the code is written as control flow, loops, and return values. R2Code treats this as a reasoning problem instead: it decomposes both sides into matching semantic layers, aligns them in two directions, and then uses the AI own explanation as evidence to check whether a link is genuine. The result is a traceability assistant that is more accurate, more portable across languages and domains, and cheaper to run, which is what practice actually needs.
Key findings
- Average F1 gain R2Code beats the strongest baseline on every dataset, with a 7.4 percent average relative F1 improvement and up to 14.1 percent on individual datasets.
- Token savings Adaptive context control cuts total token consumption by up to 41.7 percent, including a 48.0 percent drop in input tokens.
- Cost per correct link Inference cost per correctly recovered trace link falls from 0.4027 to 0.2472 USD, a 37.0 percent saving per link.
- Datasets and languages Evaluated on five public datasets covering medical, tourism, sensor network, and public administration domains in two programming languages, Java and C#.
- Ground truth scale The largest benchmark, iTrust, contains 335 requirements, 1,818 code entities, and 1,156 curated gold trace links.
- iTrust F1 score R2Code reaches F1 0.7296 versus 0.6949 for the strongest RAG plus LLM baseline, while lifting MRR from 0.8123 to 0.8550.
- Top 10 ranking quality Precision at 10 rises to 0.7720 and Recall at 10 to 0.7060, so more correct links appear earlier in the ranked list.
- Four layer alignment Requirements decompose into intent, actions, conditions, and outputs; code decomposes into function intent, control flow, variable effects, and return states.
- Component contribution Removing the alignment network, the self reflection check, or adaptive retrieval each lowers F1, confirming that all three components earn their place.
- Runtime reduction End to end runtime drops from 1234.5 to 987.3 seconds on iTrust while accuracy rises, a 20.0 percent speedup.
- Adaptive retrieval scope Low complexity requirements retrieve 8.3 candidate files on average, high complexity ones expand to 23.4, while the final prompt stays capped at 10.
- Robustness spread R2Code keeps a lower standard deviation across datasets (0.0157) than the baseline (0.0176), showing stable gains without per dataset tuning.
Three ideas that make traceable AI code links work
- Align meaning, not words: both requirement and code are split into four matching semantic layers, then compared top down and bottom up.
- Check the story: the AI writes out why a link should exist, then judges whether that explanation actually fits the requirement, and calibrates confidence accordingly.
- Spend context wisely: retrieval budgets scale with requirement complexity and reuse cached code summaries, so the model sees compact, relevant evidence.
What it means for practitioners
If you are modernising a legacy system or preparing for an audit that demands traceability, R2Code offers a practical path: it recovers requirement to code links automatically, returns an explanation for each link, and does so at a cost per correct link roughly 37 percent lower than a standard retrieval augmented generation pipeline. The explanation output matters for human review, because reviewers can accept or reject a link with visible reasons rather than trusting a bare score.
The cross language result is the strongest signal for adoption. A single fixed configuration, with no dataset specific tuning, improved results on every project including a C# codebase and four Java codebases from entirely different domains. That suggests the decomposition based approach travels well, which is exactly what teams facing mixed technology stacks need.
Related reading
Get the paper & cite it
Official citation: Yifei Wang, Jacky W. Keung, Xiaoxue Ma, Zhenyu Mao, Kehui Chen, Yishu Li (2026). R2Code: A Self-Reflective LLM Framework for Requirements-to-Code Traceability.. COMPSAC. DOI: 10.1109/COMPSAC69091.2026.00057.
