Research
Dynamic Retriever for In-Context Knowledge Editing via Policy Optimization
Overview Research area: Natural language processing — knowledge editing for large language models, specifically in-context (prompt-based) editing combined with learned retrieval and reinforcement lear
- arXiv
- 2510.21059
- Published
- 2025-10-24
- Authors
- Mahmud Wasif Nafee, Maiqi Jiang, Haipeng Chen, Yanfu Zhang
AI summary
Overview
Research area: Natural language processing — knowledge editing for large language models, specifically in-context (prompt-based) editing combined with learned retrieval and reinforcement learning. Technical level: Intermediate. The paper assumes familiarity with LLMs, in-context learning, retrieval-augmented prompting, and basic policy-gradient/reinforcement-learning concepts. Scope: The paper proposes DR-IKE, a lightweight BERT-based retriever trained with REINFORCE plus a learnable threshold ("budget controller") that selects how many supporting demonstrations to put in a prompt when editing facts in a frozen, black-box LLM.
What This Paper Is About
LLMs memorize factual knowledge from a fixed training corpus, so they often give stale or wrong answers ("the iPhone 15 is the latest model"), and retraining them is prohibitively expensive. In-context knowledge editing fixes this by putting corrected facts directly into the prompt, but existing editors pick demonstrations using static, surface-level similarity, which forces a trade-off between including enough examples and including too many noisy ones, and ignores the fact that some edits are simply harder than others. The goal of this work is to choose demonstrations by their utility for the edit rather than by similarity, and to let the number of examples grow or shrink depending on how difficult each individual edit is.
Key Contributions
- A BERT-based retriever trained with policy gradients (REINFORCE) that selects and ranks auxiliary factual demonstrations without modifying any model weights, making it usable with black-box commercial LLM APIs.
- A utility-aware selection scheme that surfaces only the most informative, non-overlapping examples, directly targeting the quantity–quality trade-off in in-context knowledge editing.
- A learnable threshold (budget controller) that tightens or relaxes retrieval based on the predicted difficulty of each edit — providing more support for abstract, temporal, or popular-entity updates while avoiding over-prompting on easy cases.
- Empirical results on the CounterFact benchmark showing higher edit success with shorter prompts and lower latency than earlier in-context editors, including an evaluation through a black-box API (Gemini-2.0-Flash) and two additional benchmarks (zsRE and WikiDataCounterFact).
Main Findings
- Edit success improves: On CounterFact, DR-IKE raises Edit Success Rate to 0.89 for both Llama-3.1-8B-Instruct and Qwen 2.5-7B, compared to 0.76 and 0.75 for the IKE baseline; the abstract reports improvements in edit success of up to 17.1%.
- Latency drops: The abstract reports a 41.6% reduction in latency. In the main results table, DR-IKE's inference time is 3.81s versus IKE's 6.52s on Llama-3.1-8B-Instruct, and 4.21s versus 6.75s on Qwen 2.5-7B.
- Overall score improves: The harmonic-mean Score (S) rises from 0.727 (IKE) to 0.775 (DR-IKE) on Llama-3.1-8B-Instruct, and from 0.738 to 0.779 on Qwen 2.5-7B. Paraphrase Consistency rises from 0.67 to 0.81 and from 0.69 to 0.77 respectively.
- Retention is slightly lower: Retention Rate falls from 0.76 to 0.66 (Llama-3.1-8B) and from 0.78 to 0.70 (Qwen 2.5-7B) relative to IKE, which the authors attribute to aggressive pruning occasionally discarding useful context.
- Higher confidence in edits: Edit Success Magnitude and Generalization Success Magnitude both improve — e.g., for Llama-3.1-8B, ESM 0.43 → 0.69 and GSM 0.39 → 0.49; for Qwen 2.5-7B, ESM 0.64 → 0.83 and GSM 0.57 → 0.74.
- Ranking alone is not enough: In the ablation, Rank-All (rank but keep everything) performs comparably to unfiltered IKE-All, while Rank-50% (static half budget) improves ESR, PC, and RR, confirming that redundant demonstrations degrade performance.
- Prompts become much shorter: IKE statically includes 16 Retain examples; DR-IKE uses a mean of 3.02 ± 0.96 Retains per prompt for LLaMA 3.1-8B and 3.72 ± 1.15 for Qwen 2.5-7B.
- Training time falls: Per-epoch runtime drops from 2080.1s to 1459.6s for Llama 3.1-8B (29.8% saving) and from 1459.6s to 1174.6s for Qwen 2.5-7B (19.5%).
- Black-box API gains are larger: With Gemini-2.0-Flash on Kaggle, DR-IKE reaches ESR 0.91 and PC 0.83 versus IKE's 0.69 and 0.52, with RR dropping from 0.57 to 0.46.
- Gains hold on other benchmarks: On zsRE, ESR rises from 0.30 (IKE) to 0.33 (DR-IKE) and PC from 0.22 to 0.26; on WikiDataCounterFact, ESR rises from 0.39 to 0.42 and PC from 0.40 to 0.43. Average k-nearest-neighbour similarity is 0.4042 for zsRE and 0.4507 for WikiDataCF, versus 0.5695 for CounterFact.
- Model scale matters: Smaller models trail on paraphrase consistency and retention — Llama 3.2 (3B) scores ESR 0.86 / PC 0.71 / RR 0.61, Qwen 2.5 (1.5B) scores 0.57 / 0.42 / 0.39, and SmolLM2 (1.7B) scores 0.46 / 0.27 / 0.22. Mistral v0.2 (7B) underperforms across all metrics (0.53 / 0.42 / 0.34), which the authors attribute to its limited 8k context window.
- The simple threshold rule beats alternatives: Tested on LLaMA-3.2-3B, the current dynamic threshold method scores ESR 0.86 / PC 0.71, outperforming MLP-on-softmax (0.81 / 0.75), MLP-on-softmax-plus-query-features (0.81 / 0.69), BERT+MLP predicting σ (0.83 / 0.65), and BERT-query-plus-context+MLP (0.80 / 0.67).
Methodology in Plain English
The framework treats "which Retain demonstrations to include" as a sequential decision problem. For each edit query, a pretrained 20M-parameter Sentence-Transformer first retrieves fixed Copy and Update demonstrations and preselects a candidate pool of Retain examples. A frozen 4-layer BERT encoder (29M parameters) with a trainable linear head scores each candidate; a softmax turns those scores into a probability distribution.
During training, the system builds the prompt incrementally, adding one Retain at a time. After each addition, it queries the frozen LLM and checks whether the answer matches the desired new fact, assigning a reward of +1 for correct and −1 for incorrect. These rewards drive a REINFORCE update that only touches the small scoring head — the underlying LLM never changes.
The budget controller is a single learnable scalar threshold σ, initialized at 0. A candidate is kept only if its policy probability exceeds σ. If adding a new Retain turns a previously correct answer into a wrong one, σ is raised to the highest probability among the remaining candidates, making the inclusion bar higher. Difficult edits therefore accumulate more examples, while easy edits terminate after a few. At inference time, this same threshold truncates the ranked list once, producing a single compact prompt for the black-box LLM.
Experiments use the CounterFact benchmark (21,919 factual records, with the first 2,000 used as the editable sample pool and the remainder for demonstrations). Training uses 300 randomly selected samples and evaluation uses 100, with the Adam optimizer at a learning rate of 1×10⁻⁴ for 5 epochs and a batch size of 1. All training ran on Google Colab with an NVIDIA L4 GPU (24 GB VRAM), implemented in PyTorch and HuggingFace Transformers v4.39.3. Baselines are FactPrompt ("Imagine that…" prefixing), EditCoT (chain-of-thought editing), and IKE (iterative Copy/Update/Retain selection). Metrics are Edit Success Rate (ESR), Retention Rate (RR), Paraphrase Consistency (PC), their harmonic mean Score (S), and Edit/Generalization Success Magnitude (ESM/GSM).
Why This Matters
Research impact: The paper reframes in-context knowledge editing as a utility-optimization problem rather than a similarity-retrieval problem, and shows that a small learned component can substantially improve a fully frozen, gradient-free editing pipeline. It also provides evidence that editing difficulty is heterogeneous and can be handled adaptively, echoing prior findings that abstract, temporal, commonsense, and popular-entity edits are harder.
Real-world applications:
- Updating factual answers in commercial chatbots served through black-box APIs, where weight access is unavailable.
- Keeping question-answering and fact-verification systems current as real-world facts change.
- Temporal reasoning and personalized recommendation systems that depend on up-to-date factual grounding.
- Deployments on limited hardware — the paper reports manageable memory footprints (extra parameters of 49M for DR-IKE versus 20M for IKE) and training on a single 24 GB GPU.
Industry relevance: Because the method requires only forward passes and no weight access, it is directly compatible with commercial LLM APIs. The reported latency and training-time savings support the paper's claim of being viable for real-time factual updates at scale, and the black-box Gemini experiment suggests larger hosted models may benefit even more from dynamic retrieval.
Future Directions
- Stabilizing Retention Rate without sacrificing the gains in edit success and paraphrase consistency, which the authors flag as a trade-off in both the open-weight and black-box experiments.
- Handling fact-type variation, since CounterFact does not categorize facts by type (historical, numerical, geographical, technical), preventing assessment of domain-specific editing performance.
- Improving performance in low-overlap or low-resource settings where sufficiently similar paraphrases and neighborhood examples are sparse or absent.
- Reducing reliance on finite context windows, since dynamic budgeting alone may not fit all necessary demonstrations for nuanced multi-step edits or extensive domain coverage.
- Exploring confidence-based calibration for the budget controller, which the authors identify as a promising direction after alternative architectures failed to beat the simple threshold rule.
Target Audience
This paper is most useful to NLP researchers and graduate students working on knowledge editing, retrieval-augmented generation, and in-context learning; to ML engineers building applications on top of frozen or black-box LLM APIs who need to inject or correct facts without fine-tuning; and to practitioners interested in applying policy-gradient methods to discrete prompt-construction decisions. Readers without background in LLM prompting and reinforcement learning will need supporting material, since the method section is written at an intermediate technical level.
Authors’ abstract
Large language models (LLMs) excel at factual recall yet still propagate stale or incorrect knowledge. In-context knowledge editing offers a gradient-free remedy suitable for black-box APIs, but current editors rely on static demonstration sets chosen by surface-level similarity, leading to two persistent obstacles: (i) a quantity-quality trade-off, and (ii) lack of adaptivity to task difficulty. We address these issues by dynamically selecting supporting demonstrations according to their utility for the edit. We propose Dynamic Retriever for In-Context Knowledge Editing (DR-IKE), a lightweight framework that (1) trains a BERT retriever with REINFORCE to rank demonstrations by editing reward, and (2) employs a learnable threshold to prune low-value examples, shortening the prompt when the edit is easy and expanding it when the task is hard. DR-IKE performs editing without modifying model weights, relying solely on forward passes for compatibility with black-box LLMs. On the COUNTERFACT benchmark, it improves edit success by up to 17.1%, reduces latency by 41.6%, and preserves accuracy on unrelated queries, demonstrating scalable and adaptive knowledge editing. The code is available at https://github.com/mwnafee/DR-IKE .