engineering is a robust idea you’ll be able to make the most of to extend the effectiveness of your LLM functions. On this article, I elaborate on context engineering strategies and the right way to succeed with AI functions using efficient context administration. Thus, if you’re engaged on AI functions using LLMs, I extremely suggest studying the total contents of the article.
I first wrote concerning the subject of context engineering in my article: How You Can Enhance LLMs with Context Engineering, the place I mentioned some context engineering strategies and vital notes. On this article, I develop on the subject by discussing extra context engineering strategies and the right way to do evaluations in your context administration.
Should you haven’t learn it already, I like to recommend you first learn my initial article on context engineering, or you’ll be able to examine ensuring reliability in LLM applications.
Desk of Contents
Motivation
My motivation for writing this text is just like my last article on context engineering. LLMs have turn into extremely vital in a whole lot of functions because the launch of ChatGPT in 2022. Nonetheless, LLMs are sometimes not utilized to their full potential as a consequence of poor context administration. Correct context administration requires context engineering abilities and strategies, which is what I’ll talk about on this article. Thus, if you’re engaged on any functions using LLMs, I extremely suggest taking notes from this text and integrating it into your individual software.
Context engineering strategies
In my final article, I mentioned context engineering strategies resembling:
- Zero/few-shot prompting
- RAG
- Instruments (MCP)
I’ll now elaborate on extra strategies which are vital to correct context administration.
Immediate structuring
With immediate structuring, I’m referring to how your immediate is organized. A messy immediate will, for instance, comprise all of the textual content with out line breaks, repetitive directions, and unclear sectioning. Take a look at the instance under for a correctly structured immediate, vs a messy immediate:
# unstructured immediate. No line breaks, repetitive directions, unclear sectioning
"You're an AI assistant specializing in query answering. You reply the customers queries in a useful, concise method, all the time making an attempt to be useful. You reply concisely, but in addition keep away from single-word solutions."
# structured immediate:
"""
## Position
You're an **AI assistant specializing in query answering**.
## Targets
1. Reply person queries in a **useful** and **concise** method.
2. At all times prioritize **usefulness** in responses.
## Fashion Pointers
- **Concise, however not overly temporary**: Keep away from single-word solutions.
- **Readability first**: Hold responses easy and straightforward to know.
- **Balanced tone**: Skilled, useful, and approachable.
## Response Guidelines
- Present **full solutions** that cowl the important data.
- Keep away from pointless elaboration or filler textual content.
- Guarantee solutions are **instantly related** to the person’s query.
"""
Immediate structuring is vital for 2 causes.
- It makes the directions clearer to the AI
- It will increase (human) readability of the immediate, which helps you detect potential points along with your immediate, keep away from repetitive directions, and so forth
You need to all the time attempt to keep away from repetitive directions. To keep away from this, I like to recommend feeding your immediate into one other LLM and asking for suggestions. You’ll sometimes obtain again a a lot cleaner immediate, with clearer directions. Anthropic additionally has a prompt generator of their dashboard, and there are additionally a whole lot of different instruments on the market to enhance your prompts.
Context window administration

One other vital level to bear in mind is context window administration. With this, I’m referring to the quantity of tokens you’re feeding into your LLM. It’s vital to do not forget that whereas latest LLMs have super-long context home windows (for example, Llama 4 Scout with a 10M context window), they aren’t essentially in a position to make the most of all of these tokens. You possibly can, for instance, read this article, highlighting how LLMs carry out worse with extra enter tokens, even when the issue of the issue stays the identical.
It’s thus vital to correctly handle your context window. I like to recommend specializing in two factors:
- Hold the immediate as brief as doable, whereas together with all related data. Look by the immediate and decide if there may be any irrelevant textual content there. If that’s the case, eradicating it’ll possible improve LLM efficiency
- You could be experiencing issues the place the LLM runs out of context window. Both due to the onerous context measurement restrict, or as a result of too many enter tokens make the LLM sluggish to reply. In these instances, it’s best to take into account context compression
For level one, it’s vital to notice that this irrelevant data is commonly not part of your static system immediate, however reasonably the dynamic data you’re feeding into the context. For instance, if you’re fetching data utilizing RAG, it’s best to take into account excluding chunks which have similarity under a selected threshold. This threshold will differ from software to software, although empirical reasoning right here sometimes works nicely.
Context compression is one other highly effective method you should utilize to correctly handle the context of your LLM. Context compression is often carried out by prompting one other LLM to summarize a part of your context. This fashion, you’ll be able to comprise the identical data utilizing fewer tokens. This strategy is, for instance, used to deal with the context window of brokers, which might shortly develop because the agent performs extra actions.
Key phrase search (vs RAG)

One other subject I believe is value highlighting is to make the most of key phrase search, along with retrieval augmented era (RAG). In most AI functions, the main target is on RAG, contemplating it might probably fetch data based mostly on semantic similarity.
Semantic similarity is tremendous highly effective as a result of in a whole lot of instances, the person doesn’t know the precise wording of what they’re on the lookout for. Looking for semantic similarity thus works very nicely. Nonetheless, in a whole lot of instances, key phrase search may also work tremendous nicely. I thus suggest integrating an choice to fetch paperwork utilizing some type of key phrase search, along with your RAG. The key phrase search will, in some eventualities, retrieve extra related paperwork than RAG is ready to.
Anthropic highlighted this strategy with their article on Contextual Retrieval from September 2024. On this article, they present you how one can make the most of BM25 to fetch related data in your RAG system successfully.
Analysis
Analysis is a vital a part of any machine-learning system. Should you don’t know the way nicely your LLMs are performing, it’s onerous to enhance your system.
Step one to analysis is observability. I thus suggest implementing immediate administration software program. You could find a sequence of such instruments on this GitHub page.
One technique to consider your context administration is to carry out A/B testing. You merely run two totally different variations of a immediate, utilizing totally different context administration strategies. Then you’ll be able to, for instance, collect person suggestions to find out which strategy works higher. One other technique to check it’s to immediate an LLM with the issue you are attempting to unravel (for instance, RAG) and the context you’re utilizing to reply the RAG question. The LLM can then offer you suggestions on the right way to enhance your context administration.
Moreover, an underrated strategy to bettering the standard of the contexts is to manually examine them. I consider a whole lot of engineers working with LLMs spend too little time on guide inspections, and analyzing the enter tokens fed into LLMs falls underneath this class. I thus suggest setting apart time to undergo a sequence of various contexts which are fed into your LLM, to find out how one can enhance. Guide inspection offers you with the chance to correctly perceive the information you’re working with and what you’re feeding into your LLMs.
Conclusion
On this article, I’ve elaborated on the subject of context engineering. Engaged on context engineering is a robust strategy to bettering your LLM software. There are a sequence of strategies you’ll be able to make the most of to raised handle the context of your LLMs, for instance, bettering your immediate construction, correct context window administration, using key phrase search, and context compression. Moreover, I additionally mentioned evaluating the contexts.
👉 Discover me on socials:
🧑💻 Get in touch
✍️ Medium