We introduce Context Language Models (CLMs), language models that natively manage their own context. We implement this by treating the context as a file and allowing the model to make unrestricted updates to this file.…

178 points•emersonmacro•about 2 months ago•52 comments•

52 comments

_jayhack_about 2 months ago
Letting a model manage its own context is very bitter lesson-pilled

Biggest challenge is you will get a much lower cache hit rate if you frequently edit the agent's context/prefix, so this can not be implemented efficiently via e.g. the Anthropic API.

This ^ can be solved in principle but likely requires modifications to the transformer architecture and definitely to serving infrastructure

See related: "KV Cache Rules Everything Around Me": https://www.completeskeptic.com/p/kv-cache-rules-everything-...

trenchgunabout 2 months ago
If you read the paper, they had a solution for this.
johnthescottabout 2 months ago
and if you didn't read the paper, they still have a solution.
metalspotabout 2 months ago
this is done on the model side so it is invisible to the caller. all of deepseeks kv cache compression stuff is basically the same thing. it doesn't really put the entire context into kv cache and does various inference side stuff to decide what to actually use as context.
wangiiabout 2 months ago
is kvcache a prematural optimization?
wat10000about 2 months ago
Not having it would be like a text editor that loads your entire document from disk and re-saves it every time you press a key. On a floppy drive.
f_devdabout 2 months ago
It's pretty much required to make it work with large contexts, so probably not
gfrecvhabout 2 months ago
Do you even understand what you're suggesting? Do you understand how the data flow would look like without a kv cache?
Neywinyabout 2 months ago
True facts. Cline does this with auto compact and it's so dumb. At the start of the session it reads the plan.md. during compaction, it forgets it. Same with its own output. It'll tell me the list of steps to take. By step 4 it needs to compact. So I prompt to do step 4. It spends 15 minutes searching the codebase and comes back that it couldn't find such a step. So dumb. So so dumb
bob1029about 2 months ago
I would be concerned with context management consuming limited attention resources.

Do you want your agent solving its own memory crisis, or do you want it solving the actual task? It can probably do both at the same time, but I suspect there is a non trivial cost associated with this.

A separate hypervisor agent that manages the main agent's context would be much better in my experience. You can run it on a different schedule and the main agent has to spend zero tokens thinking about it. This also makes it a lot easier to control when caches will be missed.

alightsoulabout 2 months ago
Maybe have A second model do the management?
theroadnotbaconabout 2 months ago
Looks like they tested that in the paper, and the code allows for it as well. https://github.com/facebookresearch/context-language-models/... Of course, this is for suggesting context management strategies rather than the actual management afaict.
verdvermabout 2 months ago
I was thinking this, not so dissimilar from co-training dflash drafters
vatsachakabout 2 months ago
Yeah, that would be the canonical solution. Modularity is better
rajveerbabout 2 months ago
An argument against having a separate hypervisor agent.

A separate hypervisor agent at least doubles the cost because (1) the underlying model needs to be the same so that we get the same degree of intelligence, and (2) it needs to have the same context + more tokens for the work it does.

Bolwinabout 2 months ago
The biggest discovery might actually be that they ignored regular caching rules and kept invalid cache suffixes and it didn't hurt performance
TeMPOraLabout 2 months ago
I wonder how bad the performance would be if they plain ignored the whole rotary encoding dance and just back-filled precisely the parts of the cache that changed directly. Would it break the model? Confuse the model? Or would the model internally correct for it?
imtringuedabout 2 months ago
You can apply rotary encoding on the file level to keep relative positioning within a file. Then there would be no global order of files (which could be an issue) but you can now add or remove whole files as you like.
wren6991about 2 months ago
Kimi K3 is an example of a modern LLM that doesn't use positional encodings. It uses NoPE'd MLA for global attention and a variant of Gated DeltaNet (KDA) for local attention. I wonder how much the KDA would degrade if you just did a bounded replay over the last few thousand tokens to recover its approximate state when assembling your context window from chunks of known MLA KV.
dist-epochabout 2 months ago
The rotary issue is a simple rotation, cheap, no reason to skip it.

But this is the kind of thing you could ask your agent to test locally.

svachalekabout 2 months ago
Wow. Context management is one of the big remaining hassles with modern LLMs so this could be big. The obvious complication is cache busting so it's also exciting they investigated solutions for that.
visargaabout 2 months ago
Can't we do this trick today with any model? Just send the file as next context. Of course you pay the price for cache misses, depending how deep you make changes, while CLM just ignores the recomputation.
nsingh2about 2 months ago
One approximation of this is the experimental context management Codex has been moving towards (not released yet). Rather than relying on summary compaction, the model maintains notes as it works and as it approaches the context limit. A new session is just a fresh context with those notes attached, and a pointer back to the previous session.

Not exactly like what this paper is suggesting, but similar in the sense it lets the model decide what and how to persist across turns.

I recreated this in Pi, with a max token limit on how long the note can be, to pressure the model to be concise. Ends up being cheaper than summary compaction too.

TeMPOraLabout 2 months ago
Interesting. It matches my manual workflow with all harnesses (including vanilla web ChatGPT/Gemini/Claude) for the past year or so: when the session gets compacted, or (ideally) when I feel it's about to be, I just tell it to write a handover note, and start a new session.

With some specific workflow I use in some cases (involving leaving long-lived intermediary artifacts), this turned into me pasting a path to handover file in previous agent's session, and handover itself directs the agent to key files from that session to read, and that's it. So far, with this process, at no point I felt any quality degradation (though early on I often see "I need to check how my predecessor did ${something}", followed by surgical spelunking of past chat's history), even as I carry a single piece of complex analytical work over 5+ sessions.

veqqabout 2 months ago
Claude Roux uses a refined version:

> LLMs have a lot of knowledge but few competencies. If you constrain them to output knowledge and use that to further constrain results, you’ll go far. For context management, I have the system generate `log.jsonl` and `log.py` (which queries the other document). Whenever an action is processed (an error’s corrected etc.) the system adds something to `log.jsonl`. If it needs to know what happens, it uses `log.py` to query and display only the relevant/required information (like a date, errors or attempted fixes) reducing tokens. - https://alexalejandre.com/interviews/interview-with-claude-r...

visargaabout 2 months ago
That is similar to what I am thinking... not just edit the context as a file or string, but have a way to evict blocks and replace them with summary notes and also be able to retrieve them on demand.

Do you have a public repo for your approach?

ijidakabout 2 months ago
Memento
verdvermabout 2 months ago
yup, I build a set of fs tools in my custom coding harness that worked like this, doing it again in another custom harness that I only expect to take one turn per request, you can still get decent caching by ordering things so most dynamic comes later

Read the full thread on Hacker News →

Related stories