The longer the context of the coding agent, the higher the cost of each call, can the context be compressed without affecting the completion of the task?
Coding Agent solves software problems by repeatedly checking the code, modifying the program, running the test: each step returns the contents of the file, search results, and logs, which remain in the context of the next call. The more difficult the task, the longer the context, the more tokens to be processed, and the higher the cost. Statistics found that on SWE-bench Verified, these observations accounted for 45.7% of total token consumption, reaching a peak of 67.8% on Terminal-Bench.
The difficulty is: it is difficult to know if the content that has been deleted will continue to be used when it is compressed.
In 200 instances of SWE-bench Verified, three Agent models, CoACT reduced total token consumption by an average of 33.0%, with an average PASS@1 decreasing from 69.0% to 71.0%, which is close to the task performance when uncompressed. This work has been directly received by CCF Class A conference ICSE 2027 (Top 5%).

CoACT: Action-Preserving Observation Compression for Coding Agents
Link to the paper:https://arxiv.org/abs/2607.02911
Code address:https://github.com/THU-Agent/CoACT
A: Limitations of existing compression methods
One way is to rewrite history: the slider window loses earlier commands and output, and AgentDiet allows the model to periodically review the history, removing repetitive or unusable content itself. However, once the history is rewritten, the original KV Cache cannot be fully replicated, the model must replenish the content after the rewriting, tokens are reduced, and expenses are not necessarily reduced.
The other way is to compress before the new observation enters the context. The previous history is unlocked and cached as usual. CoACT chose the KV Cache friendly way.
The goal is to minimize total token consumption as much as possible, provided the task success rate ((PASS@1) is close to the uncompressed Agent).
This needs to be balanced in two respects: if too much is removed, the agent may lack the necessary information, causing the task to fail or be read repeatedly; if too little is removed, costs cannot be effectively reduced.
The second key idea: the next step is to keep the NAP.
The most direct method of verification is to have the Agent read the compression results and run the task. However, the same observation can have many types of compression, each running at full speed, and the cost is too high; a task must be compressed many times at a time, and it is difficult to determine which of the final results is caused by the compression.
We believe that the execution process of an agent can be seen as a cycle in which the agent reads the output, performs the action, obtains a new output. Assuming that the initial environment is the same and the environment responds to the same action, the sequence of actions and the final result will theoretically not change as long as the next action of the agent after each compression remains the same and the results of the actions received and returned by the environment remain the same.
Based on this guideline, we propose the next action to keep the next-action preservation (NAP): keep the task goal and history the same, respectively, allowing the agent to read the observation before and after the compression and then compare it to its next action. For example, after reading a piece of code, the agent wants to see a function called inside, and instead of the compression result, it wants to see this function, so that the compression remains the next step. This allows for individual checks on each compression without waiting for the task to end.
Three, how to create training data:
Compressed demonstration architecture based on NAP
With the NAP, we trained a lightweight compressor with Qwen3.5-4B as the base.
First, let the instructor model Gemini3-Flash produce 8 compression candidates for the same observation; then let the Agent sample multiple times under the original observation and get 8 reference moves. Each compression resulting from the action will be compared with the reference move one by one, taking the 3 most similar request averages, and scoring 0.6 to pass. When compared, the operation type of the command is not the string, but the task of analyzing the target file and reading range, the commands are written differently but read the same code, and will be identified as the same operation.
After checking, a shorter version is selected as a training demonstration. If all eight candidates fail, use the original observation as the goal, so that the model learns to stay when it should.
Four, two stages of training: Offline guidance and online alignment
There is a disadvantage to using only the uncompressed trajectory data of the Agent: when the data is collected, the history of the Agent is complete; after the actual deployment, the previous few steps read are also compressed and the history seen is different.
The practice of CoACT is to: in the first phase, supervise off-line trajectory and obtain the initial compressor; in the second phase, allow it to actually participate in the operation of the Agent, retaining the previously compressed history, re-comparing movements and length filtering, obtaining the second batch of data; and finally, combine the two stages of data training with the final compressor.

In the control experiment, compressors trained with offline data only, PASS@1 was 56.0%, slightly lower than the uncompressed 57.0%; after adding the online data in the second phase, it rose to 60.5%, and total token consumption was basically equivalent.

Five: Test results: Save a third and the performance is unchanged.
In 200 instances of SWE-bench Verified random drawing, CoACT reduced token consumption on all three models of Qwen 3.5-35B-A3B, Deepseek-v4-Pro and Gemini 3-Flash: Qwen decreased from 3.795M to 2.428M, Deepseek decreased from 1.068M to 0.868M, Gemini decreased from 0.685M to 0.422M, an average decrease of 33.0%.

After the compression, the longer and more localized observations became less noticeable: on Qwen 3.5-35B-A3B, the observation share of over 1K tokens decreased from 32.3% to 9.4%.
Compared to existing observation compression methods, the gap is more significant. In Qwen, for example, after LLMLingua-2 compression, PASS@1 decreases from 57.0% to 50.0%, while the total token decreases from 3.795M to 10.329M; SWE-Pruner decreases to 4.471M; LongCodeZip decreases to 3.165M; CoACT to 2.428M, PASS@1 to 60.5%. On Deepseek, the total tokens for all three baselines are higher than the uncompressed 1.068M, while CoACT decreases to 0.868M, the lowest of the two rating models.

As a single-step compression method, CoACT can also be combined with the historical compression method. While using AgentDiet alone, the total token has decreased, and the estimated total cost has never been compressed, rising to $41.26 to $45.65, due to the history rewrite affecting KV Cache replication. Before joining CoACT in the AgentDiet process, the estimated total cost has dropped to $25.88, and the mission success rate has remained close, about 43.3% lower than using AgentDiet alone.

Introduction by the author
This study was completed by the AI Agent group at the Institute of Artificial Intelligence of the University of China. The author of the communication is assistant professor Li Xia of the Institute of Artificial Intelligence of the University of China, the main research direction is AI + software, credible intelligent agents.









