has acquired critical consideration with the rise of LLMs able to dealing with advanced duties. Initially, most discussions on this speak revolved round immediate engineering: Tuning a single immediate for optimized efficiency on a single activity. Nonetheless, as LLMs develop extra succesful, immediate engineering has became context engineering: Optimizing all knowledge you feed into your LLM, for max efficiency on advanced duties.
On this article, I’ll dive deeper into agentic context engineering, which is about optimizing the context particularly for brokers. This differs from conventional context engineering in that brokers usually carry out sequences of duties for an extended time frame. Since agentic context engineering is a big subject, I’ll dive deeper into the matters listed under on this article and write a follow-up article masking extra matters.
- Particular context engineering ideas
- Shortening/summarizing the context
- Software utilization
Why care about agentic context engineering

Earlier than diving deeper into the specifics of context engineering, I’ll cowl why agentic context engineering is vital. I’ll cowl this in two components:
- Why we use brokers
- Why brokers want context engineering
Why we use brokers
To begin with, we use brokers as a result of they’re extra able to performing duties that static LLM calls. Brokers can obtain a question from a person, for instance:
Repair this user-reported bug {bug report}
This may not be possible inside a single LLM name, since that you must perceive the bug higher (possibly ask the one that reported the bug), that you must perceive the place within the code the bug happens, and possibly fetch a few of the error messages. That is the place brokers are available in.
An agent can take a look at the bug, name a instrument asking the person a follow-up query, for instance: The place within the software does this bug happen? The agent can then discover that location within the codebase, run the code itself to learn error logs, and implement the repair. This all requires a collection of LLM calls and instruments calls earlier than fixing the difficulty.
Why brokers want context engineering
So we now know why we’d like brokers, however why do brokers want context engineering? The principle purpose is that LLMs all the time carry out higher when their context accommodates extra related info and fewer noise (irrelevant info). Moreover, brokers’ context rapidly provides up after they carry out a collection of instrument calls, for instance, fetching the error logs when a bug occurs. This creates context bloat, which is when the context of an LLM accommodates quite a lot of irrelevant info. We have to take away this noisy info from the LLMs context, and likewise guarantee all related info is current within the LLMs context.
Particular context engineering ideas
Agentic context engineering builds on high of conventional context engineering. I thus embody a number of vital factors to enhance your context.
- Few-shot studying
- Structured prompts
- Step-by-step reasoning
These are three generally used strategies inside context engineering that usually enhance LLM efficiency.
Few-shot studying
Few-shot studying is a generally used strategy the place you embody examples of an analogous activity earlier than feeding the agent the duty it’s to carry out. This helps the mannequin perceive the duty higher, which often will increase efficiency.
Under you’ll be able to see two immediate examples. The primary instance exhibits a zero-shot immediate, the place we straight ask the LLM the query. Contemplating it is a easy activity, the LLM will probably get the correct reply; nevertheless, few-shot studying can have a higher impact on harder duties. Within the second immediate, you see that I present a number of examples on the best way to do the maths, the place the examples are additionally wrapped in XML tags. This not solely helps the mannequin perceive what activity it’s performing, but it surely additionally helps guarantee a constant reply format, for the reason that mannequin will usually reply in the identical format as supplied within the few-shot examples.
# zero-shot
immediate = "What's 123+150?"
# few-shot
immediate = """
"What's 10+20?" -> "30"
"What's 120+70?" -> "190"
What's 123+150?
"""
Structured prompts
Having structured prompts can also be an extremely vital a part of context engineering. Within the code examples above, you’ll be able to see me utilizing XML tags with
You should use designated instruments like Anthropic’s immediate optimizer, however you may also merely feed your unstructured immediate into ChatGPT and ask it to enhance your immediate. Moreover, you’ll get even higher prompts if you happen to describe eventualities the place your present immediate is struggling.
For instance, you probably have a math agent that’s doing rather well as well as, subtraction, and division, however scuffling with multiplication, you must add that info to your immediate optimizer.
Step-by-step reasoning
Step-by-step reasoning is one other highly effective context engineering strategy. You immediate the LLM to suppose step by stepabout the best way to remedy the issue, earlier than making an attempt to unravel the issue. For even higher context engineering, you’ll be able to mix all three approaches coated on this part, as seen within the instance under:
# few-shot + structured + step-by-step reasoning
immediate = """
"What's 10+20?" -> "To reply the person request, I've so as to add up the 2 numbers. I can do that by first including the final two digits of every quantity: 0+0=0. I then add up the final two digits and get 1+2=3. The reply is: 30"
"What's 120+70?" -> "To reply the euser request, I've so as to add up the digits going backwards to entrance. I begin with: 0+0=0. Then I do 2+7=9, and at last I do 1+0=1. The reply is: 190"
What's 123+150?
"""
It will assist the mannequin perceive the examples even higher, which regularly will increase mannequin efficiency even additional.
Shortening the context
When your agent has operated for a number of steps, for instance, asking for person enter, fetching some info, and so forth, you may expertise the LLM context filling up. Earlier than reaching the context restrict and dropping all tokens over this restrict, you must shorten the context.
Summarization is an effective way of shortening the context; nevertheless, summarization can typically reduce out vital items of your context. The primary half of your context may not comprise any helpful info, whereas the second half contains a number of paragraphs which are required. That is a part of why agentic context engineering is troublesome.
To carry out context shortening, you’ll usually use one other LLM, which I’ll name the Shortening LLM. This LLM receives the context and returns a shortened model of it. The best model of the Shortening LLM merely summarizes the context and returns it. Nonetheless, you’ll be able to make use of the next strategies to enhance the shortening:
- Decide if some complete components of the context will be reduce out (particular paperwork, earlier instrument calls, and so on)
- A prompt-tuned Shortening LLM, optimized for analyzing the duty at hand, all related info accessible, and returns solely the data that can be related to fixing the duty
Decide if complete components will be reduce out
The very first thing you must do when making an attempt to shorten the context is to search out areas of the context that may be fully reduce out.
For instance, if the LLM may’ve beforehand fetched a doc, used to unravel a earlier activity, the place you’ve the duty outcomes. This implies the doc is just not related anymore and must be faraway from the context. This may additionally happen if the LLM has fetched different info, for instance by way of key phrase search, and the LLM has itself summarized the output of the search. On this occasion, you must take away the previous output from the key phrase search.
Merely eradicating such complete components of the context can get you far in shortening the context. Nonetheless, that you must take into account that eradicating context that may be related for later duties will be detrimental to the agent’s efficiency.
Thus, as Anthropic factors out of their article on context engineering, you must first optimize for recall, the place you make sure the LLM shortener by no means removes context that’s related sooner or later. Once you obtain virtually good recall, you can begin specializing in precision, the place you take away increasingly context that’s not related anymore to fixing the duty at hand.

Immediate-tuned shortening LLM
I additionally advocate making a prompt-tuned shortening LLM. To do that, you first must create a take a look at set of contexts and the specified shortened context, given a activity at hand. These examples ought to ideally be fetched from actual person interactions along with your agent.
Persevering with, you’ll be able to immediate optimize (and even fine-tune) the shortening LLM for the duty of summarizing the LLM’s context, to maintain vital components of the context, whereas eradicating different components of the context that aren’t related anymore.
Instruments
One of many details separating brokers from one-off LLM calls is their use of instruments. We usually present brokers with a collection of instruments they’ll use to extend the agent’s means to unravel a activity. Examples of such instruments are:
- Carry out a key phrase search on a doc corpus
- Fetch details about a person given their electronic mail
- A calculator so as to add numbers collectively
These instruments simplify the issue the agent has to unravel. The agent can carry out a key phrase search to fetch extra (usually required) info, or it might use a calculator so as to add numbers collectively, which is way more constant than including numbers utilizing next-token prediction.
Listed below are some strategies to bear in mind to make sure correct instrument utilization when offering instruments within the agent’s context:
- Effectively-described instruments (can a human perceive it?)
- Create particular instruments
- Keep away from bloating
- Solely present related instruments
- Informative error dealing with
Effectively-described agentic instruments
The primary, and possibly most vital word, is to have well-described instruments in your system. The instruments you outline ought to have sort annotations for all enter parameters and a return sort. It also needs to have an excellent perform identify and an outline within the docstring. Under you’ll be able to see an instance of a poor instrument definition, vs an excellent instrument definition:
# poor instrument definition
def calculator(a, b):
return a+b
# good instrument definition
def add_numbers(a: float, b: float) -> float:
"""A perform so as to add two numbers collectively. Must be used anytime you need to add two numbers collectively.
Takes in parameters:
a: float
b: float
Returns
float
"""
return a+b
The second perform within the code above is way simpler for the agent to know. Correctly describing instruments will make the agent a lot better at understanding when to make use of the instrument, and when different approaches is healthier.
The go-to benchmark for a well-described instrument is:
Can a human who has by no means seen the instruments earlier than, perceive the instruments, simply from wanting on the capabilities and their definitions?
Particular instruments
You also needs to attempt to preserve your instruments as particular as attainable. Once you outline obscure instruments, it’s troublesome for the LLM to know when to make use of the instrument and to make sure the LLM makes use of the instrument correctly.
For instance, as an alternative of defining a generic instrument for the agent to fetch info from a database, you must present particular instruments to extract particular information.
Dangerous instrument:
- Fetch info from database
- Enter
- Columns to retrieve
- Database index to search out information by
Higher instruments:
- Fetch information about all customers from the database (no enter parameters)
- Get a sorted checklist of paperwork by date belonging to a given buyer ID
- Get an combination checklist of all customers and the actions they’ve taken within the final 24 hours
You’ll be able to then outline extra particular instruments if you see the necessity for them. This makes it simpler for the agent to fetch related info into its context.
Keep away from bloating
You also needs to keep away from bloating in any respect prices. There are two most important approaches to reaching this with capabilities:
- Capabilities ought to return structured outputs, and optionally, solely return a subset of outcomes
- Keep away from irrelevant instruments
For the primary level, I’ll once more use the instance of a key phrase search. When performing a key phrase search, for instance, towards AWS Elastic Search, you’ll obtain again quite a lot of info, typically not that structured.
# unhealthy perform return
def keyword_search(search_term: str) -> str:
# carry out key phrase search
# outcomes: {"id": ..., "content material": ..., "createdAt": ..., ...}, {...}, {...}]
return str(outcomes)
# good perform return
def _organize_keyword_output(outcomes: checklist[dict], max_results: int) -> str:
output_string = ""
num_results = len(outcomes)
for i, res in enumerate(outcomes[:max_results]): # max return max_results
output_string += f"Doc quantity {i}/{num_results}. ID: {res["id"]}, content material: {res["content"]}, created at: {res["createdAt"]}"
return output_string
def keyword_search(search_term: str, max_results: int) -> str:
# carry out key phrase search
# outcomes: {"id": ..., "content material": ..., "createdAt": ..., ...}, {...}, {...}]
organized_results = _organize_keyword_output(outcomes, max_results)
return organized_results
Within the unhealthy instance, we merely stringify the uncooked checklist of dicts returned from the key phrase search. The higher strategy is to have a separate helper perform to construction the outcomes right into a structured string.
You also needs to make sure the mannequin can return solely a subset of outcomes, as proven with the max_results parameter. This helps the mannequin rather a lot, particularly with capabilities like key phrase search, that may probably return 100’s of outcomes, instantly filling up the LLM’s context.
My second level was on avoiding irrelevant instruments. You’ll most likely encounter conditions the place you’ve quite a lot of instruments, lots of which is able to solely be related for the agent to make use of at particular steps. If you realize a instrument is just not related for an agent at a given time, you must preserve the instrument out of the context.
Informative error dealing with
Informative error dealing with is crucial when offering brokers with instruments. It is advisable to assist the agent perceive what it’s doing mistaken. Normally, the uncooked error messages supplied by Python are bloated and never that simple to know.
Under is an effective instance of error dealing with in instruments, the place the agent is informed what the error was and the best way to take care of it. For instance, when encountering charge restrict errors, we inform the agent to particularly sleep earlier than making an attempt once more. This simplifies the issue rather a lot for the agent, because it doesn’t should purpose itself that it has to sleep.
def keyword_search(search_term: str) -> str:
strive:
# key phrase search
outcomes = ...
return outcomes
besides requests.exceptions.RateLimitError as e:
return f"Charge restrict error: {e}. You must run time.sleep(10) earlier than retrying."
besides requests.exceptions.ConnectionError as e:
return f"Connection error occurred: {e}. The community is perhaps down, inform the person of the difficulty with the inform_user perform."
besides requests.exceptions.HTTPError as e:
return f"HTTP error occurred: {e}. The perform failed with http error. This often occurs due to entry points. Make sure you validate earlier than utilizing this perform"
besides Exception as e:
return f"An sudden error occurred: {e}"
You must have such error dealing with for all capabilities, maintaining the next factors in thoughts:
- Error messages must be informative of what occurred
- If you realize the repair (or potential fixes) for a particular error, inform the LLM the best way to act if the error happens (for instance: if a charge restrict error, inform the mannequin to run time.sleep())
Agentic context engineering going ahead
On this article, I’ve coated three most important matters: Particular context engineering ideas, shortening the brokers’ context, and the best way to present your brokers with instruments. These are all foundational matters that you must perceive to construct an excellent AI agent. There are additionally additional matters that you must be taught extra about, such because the consideration of pre-computed info or inference-time info retrieval. I’ll cowl this subject in a future article. Agentic context engineering will proceed to be a brilliant related subject, and understanding the best way to deal with the context of an agent is, and can be, elementary to future AI agent developments.
👉 Discover me on socials:
🧑💻 Get in contact
✍️ Medium
You may also learn a few of my different articles: