DEV Community

Salvatore Attaguile
Salvatore Attaguile

Posted on

Context-Anchored Generation (CAG): Fixing Hallucinations at the Decoding Layer

Hallucination isn’t an output problem.
It’s a generation problem.

The Problem Isn’t Knowledge — It’s Control

Large language models don’t hallucinate because they “don’t know.”

They hallucinate because generation drifts.

At each step, the model predicts:

P(tokenₜ | context)

That context is constantly shifting.

Over time, something subtle happens:

  • The original prompt weakens
  • Recent tokens dominate
  • High-frequency patterns take over

This creates what can be described as semantic drift.

The model doesn’t suddenly “break.”

It gradually leaves the frame.

The Core Idea

CAG introduces a simple constraint:

Every token must stay semantically aligned with a persistent frame.

Instead of letting generation run open-loop, we:

  1. Create a semantic anchor from the prompt
  2. Track how far each new token drifts
  3. Intervene during decoding, not after

Two-State Decoding

CAG operates as a control system with two modes:

  1. Constraint Mode

    • Enforces alignment with the anchor
    • Penalizes tokens that drift too far
    • Keeps generation stable
  2. Expansion Mode

    • Allows controlled divergence
    • Triggers when drift is detected
    • Lets the model explore without losing structure

This is not filtering.

This is governed generation.

The Key Signal: Drift

For each candidate token, we compute:

δ = 1 − cosine_similarity(token, frame)

  • δ = 0 → perfectly aligned
  • δ ≈ 1 → unrelated
  • δ → 2 → opposite

We also track accumulated drift over time.

This matters because:

Hallucination is not one bad token — it’s many small deviations adding up.

What Makes CAG Different

Most systems operate around generation:

  • RAG → adds context
  • RLHF → shapes training
  • Filters → remove bad outputs

CAG operates inside generation itself.

It sits between:

logits → sampling

And governs what gets selected.

What It Fixes (Beyond Hallucination)

Because everything flows from drift, CAG also stabilizes:

  • Repetition loops
  • Long-context degradation
  • Premature topic switching
  • Generic filler outputs

Same mechanism. No extra logic.

How to Think About It

Every LLM today runs open-loop.

CAG closes the loop.

The model no longer just generates.

It checks itself as it generates.

Performance

  • No retraining required
  • Model-agnostic
  • ~0.003% overhead per token
  • Works with standard HuggingFace pipelines

This isn’t theoretical — it’s implementable.

Try It Yourself

Paper + Code (Zenodo DOI): https://doi.org/10.5281/zenodo.18912274

Where It Breaks

This matters.

CAG assumes semantic continuity is desirable.

It will struggle with:

  • Abstract poetry
  • Surreal writing
  • Rapid topic jumping

Because in those cases: drift is intentional

Final Thought

We’ve been treating hallucination as a knowledge problem.

It’s not.

It’s a control problem.

And control belongs at the point where decisions are made:

decoding

If you test this and it fails, I want to hear about it.

If it holds, that’s more
By: Salvatore Attaguile
Forest Code Labs

Top comments (1)

Collapse
 
salvatore_attaguile_afcf8b44 profile image
Salvatore Attaguile

If you’re working with LLMs, I’m genuinely curious where this breaks.

Not hypotheticals — real edge cases.

Especially:
• long context generation

• code tasks

• multi-turn systems

I’m less interested in “this sounds good”

and more in:

“this failed here”

That’s where the model gets sharper.