Why do you understand the concept but can’t write the context?
In the AI Coding Assistant, Context Engineering (context engineering) is often described as "how to provide the most relevant information to the model". But when you open the editor, the question immediately becomes: Which files should be included? How much of the token's history should be included? Do you want to feed the error log together?
The first and most common stuck point is: you treat Context Engineering as model fine-tuning. In fact, it has nothing to do with training, only how to organize the existing information in the current dialogue or Agent loop. This means you don’t need a GPU, and you don’t need to retrain, just a structured set of selection, injection, and update rules.
The second sticking point is: you try to cram the entire code base into it at once. Many first-timers will splice all the directory structure, README, and dozens of files in the project. The result is that either the token window is exceeded, or the model cannot grasp the key points at all.
The first step: first build the content injection layer
A valid Context Engineering requires three layers: acquisition layer, filtering layer, and injection layer. The first layer directly determines the subsequent quality, but most people pay more attention to the last layer.
The core of the acquisition layer is "active acquisition rather than passive splicing". In a coding scenario, you need to clearly define what data is required:
- The currently open file and its last 20-50 lines of changes (rather than the entire file history)
- Live output of current compilation errors or linter warnings (skip if none)
- A brief description of the most recent git diff (if needed to understand contextual changes)
- Key nouns and actions in the user’s last natural language command
A more specific approach is to set up a file change listener (such as chokidar) to trigger a context snapshot when the file is saved, rather than refreshing every keyboard stroke. This not only reduces API consumption, but also prevents the model from "amnesia" due to context mutations midway through the answer.

Step 2: Define the context validity period and elimination mechanism
This is the easiest part to fail. Many people will design a perfect context structure, but forget to consider the cleanup of "outdated context". The resulting model's every answer referenced a function that had been removed or an out-of-date annotation.
Failure scenario 1: You saved the context of an error message. The user subsequently modified the code and recompiled it, but the previous error context still occupied the token position, causing the new model response to mistakenly believe that the error still existed.
Workaround: Add a "valid until" tag to each context. For example, the compilation error context is automatically removed only after the next successful compilation operation; the git diff context is cleared after the current branch is switched or committed. You can maintain this with a simple priority queue: high priorities (such as the user's current command) are always retained, and low priorities expire by time or event.
Another common problem is having too many contexts. Even if each one is very accurate, if more than 10 are injected at once, the model will start to suffer from attention dilution. It is generally recommended to keep 3-5 backbone contexts, and load the rest through user active queries.

Step 3: Minimum implementation path that can be implemented
Here is a classic design that does not require a special framework and is only based on Node.js or Python:
- Create a
contextEngine.js, including aContextStoreclass, and useMapinternally to store each context, and the key is an automatically generated ID. - Implement three methods:
addContext({ id, content, priority, expireOnEvent }),removeContext(id),getActiveContexts(). - Call in the "tool call" or "hook" of your AI coding assistant (such as Continue, Aider, Cursor Agent): when the file is saved, call
addContextto record the diff; when the compilation run ends, check for errors and update the corresponding context. - In the prompt assembly function sent to the model, call
getActiveContexts()to splice in descending order of priority.
There's a real trade-off here: do you want "as much context as possible" or "only give the model what it needs most"? The former can easily confuse the model, while the latter requires you to define a set of rules yourself. Based on our practice, we recommend the latter, and the rules should be less rather than more. Start with a simple "recently modified files + latest errors" and then gradually increase it.
Step 4: Diagnosis and rollback in case of failure
If the model answer quality decreases, first check whether the context queue is overloaded or out of date. It is recommended to output the context status log in the terminal (using a short JSON line) to facilitate review. One more checkpoint: watch to see if the model repeats a problem it has solved before. This is usually a sign that the context was not cleared in a timely manner.
If the above problem occurs frequently, you can fall back to a narrower context strategy: only include the current file and a user command, and abandon incremental accumulation completely. Although the model will lack a global vision, at least the answer stability is higher. This is an acceptable engineering trade-off, especially if you require high availability.
From practice to system improvement
After running through the above minimum path, you may find that there are many optimization points, such as how to save context templates across sessions and how to switch different context strategies for different tasks (refactoring, debugging, code review). These are already advanced topics and are difficult to exhaust in a single article.
If your goal is to break away from the "API adjustment" style and truly master how to design an Agent-level context closed loop, it is recommended to enter a more systematic learning path: starting from environment construction, to fine-tuning the context injection strategy, and then to the context sharing mechanism under multi-Agent collaboration.

No comments yet. Be the first to share your thoughts.