has obtained critical consideration with the rise of LLMs able to dealing with complicated duties. Initially, most discussions on this discuss revolved round immediate engineering: Tuning a single immediate for optimized efficiency on a single process. Nonetheless, as LLMs develop extra succesful, immediate engineering has was context engineering: Optimizing all knowledge you feed into your LLM, for optimum efficiency on complicated 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 matter, I’ll dive deeper into the matters listed beneath on this article and write a follow-up article overlaying extra matters.
- Particular context engineering ideas
- Shortening/summarizing the context
- Instrument 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 essential. I’ll cowl this in two components:
- Why we use brokers
- Why brokers want context engineering
Why we use brokers
Initially, 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 consumer, for instance:
Repair this user-reported bug {bug report}
This is able to not be possible inside a single LLM name, since you have to perceive the bug higher (perhaps ask the one who reported the bug), you have to perceive the place within the code the bug happens, and perhaps fetch a number of the error messages. That is the place brokers are available.
An agent can take a look at the bug, name a software asking the consumer 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 want brokers, however why do brokers want context engineering? The principle cause is that LLMs at all times carry out higher when their context incorporates extra related info and fewer noise (irrelevant info). Moreover, brokers’ context shortly provides up once they carry out a collection of software calls, for instance, fetching the error logs when a bug occurs. This creates context bloat, which is when the context of an LLM incorporates a variety 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 couple of essential 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 identical process earlier than feeding the agent the duty it’s to carry out. This helps the mannequin perceive the duty higher, which normally will increase efficiency.
Beneath you possibly can see two immediate examples. The primary instance exhibits a zero-shot immediate, the place we instantly ask the LLM the query. Contemplating this can be a easy process, the LLM will seemingly get the proper reply; nevertheless, few-shot studying can have a better impact on harder duties. Within the second immediate, you see that I present a couple of examples on methods to do the maths, the place the examples are additionally wrapped in XML tags. This not solely helps the mannequin perceive what process it’s performing, nevertheless it additionally helps guarantee a constant reply format, because the mannequin will usually reply in the identical format as offered 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 be an extremely essential a part of context engineering. Within the code examples above, you possibly can see me utilizing XML tags with
You need to use designated instruments like Anthropic’s immediate optimizer, however you can too merely feed your unstructured immediate into ChatGPT and ask it to enhance your immediate. Moreover, you’ll get even higher prompts when you describe eventualities the place your present immediate is struggling.
For instance, you probably have a math agent that’s doing very well as well as, subtraction, and division, however scuffling with multiplication, you need to 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 methods to remedy the issue, earlier than making an attempt to unravel the issue. For even higher context engineering, you possibly can mix all three approaches coated on this part, as seen within the instance beneath:
# few-shot + structured + step-by-step reasoning
immediate = """
"What's 10+20?" -> "To reply the consumer 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 eventually I do 1+0=1. The reply is: 190"
What's 123+150?
"""
It will assist the mannequin perceive the examples even higher, which frequently will increase mannequin efficiency even additional.
Shortening the context
When your agent has operated for a couple of steps, for instance, asking for consumer enter, fetching some info, and so forth, you would possibly expertise the LLM context filling up. Earlier than reaching the context restrict and shedding all tokens over this restrict, you need to shorten the context.
Summarization is an effective way of shortening the context; nevertheless, summarization can generally lower out essential items of your context. The primary half of your context may not comprise any helpful info, whereas the second half consists of 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 only model of the Shortening LLM merely summarizes the context and returns it. Nonetheless, you possibly can make use of the next strategies to enhance the shortening:
- Decide if some entire components of the context might be lower out (particular paperwork, earlier software calls, and so forth)
- A prompt-tuned Shortening LLM, optimized for analyzing the duty at hand, all related info accessible, and returns solely the data that shall be related to fixing the duty
Decide if entire components might be lower out
The very first thing you need to do when making an attempt to shorten the context is to search out areas of the context that may be utterly lower out.
For instance, if the LLM would possibly’ve beforehand fetched a doc, used to unravel a earlier process, the place you could have the duty outcomes. This implies the doc isn’t 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 need to take away the previous output from the key phrase search.
Merely eradicating such entire components of the context can get you far in shortening the context. Nonetheless, you have to remember the fact that eradicating context that may be related for later duties might be detrimental to the agent’s efficiency.
Thus, as Anthropic factors out of their article on context engineering, you need to first optimize for recall, the place you make sure the LLM shortener by no means removes context that’s related sooner or later. While you obtain virtually good recall, you can begin specializing in precision, the place you take away increasingly more context that isn’t related anymore to fixing the duty at hand.

Immediate-tuned shortening LLM
I additionally suggest making a prompt-tuned shortening LLM. To do that, you first have to create a take a look at set of contexts and the specified shortened context, given a process at hand. These examples ought to ideally be fetched from actual consumer interactions along with your agent.
Persevering with, you possibly can immediate optimize (and even fine-tune) the shortening LLM for the duty of summarizing the LLM’s context, to maintain essential 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 skill to unravel a process. Examples of such instruments are:
- Carry out a key phrase search on a doc corpus
- Fetch details about a consumer given their e 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 will possibly 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 software 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 doubtless most essential notice, 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 must also have a superb operate identify and an outline within the docstring. Beneath you possibly can see an instance of a poor software definition, vs a superb software definition:
# poor software definition
def calculator(a, b):
return a+b
# good software definition
def add_numbers(a: float, b: float) -> float:
"""A operate so as to add two numbers collectively. Ought to be used anytime you need to add two numbers collectively.
Takes in parameters:
a: float
b: float
Returns
float
"""
return a+b
The second operate within the code above is far simpler for the agent to know. Correctly describing instruments will make the agent significantly better at understanding when to make use of the software, and when different approaches is best.
The go-to benchmark for a well-described software is:
Can a human who has by no means seen the instruments earlier than, perceive the instruments, simply from wanting on the features and their definitions?
Particular instruments
You must also attempt to preserve your instruments as particular as doable. While you outline obscure instruments, it’s troublesome for the LLM to know when to make use of the software and to make sure the LLM makes use of the software correctly.
For instance, as an alternative of defining a generic software for the agent to fetch info from a database, you need to present particular instruments to extract particular data.
Unhealthy software:
- Fetch info from database
- Enter
- Columns to retrieve
- Database index to search out data by
Higher instruments:
- Fetch data 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 mixture 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 once 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 must also keep away from bloating in any respect prices. There are two predominant approaches to attaining this with features:
- Features 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, in opposition to AWS Elastic Search, you’ll obtain again a variety of info, generally not that structured.
# unhealthy operate return
def keyword_search(search_term: str) -> str:
# carry out key phrase search
# outcomes: {"id": ..., "content material": ..., "createdAt": ..., ...}, {...}, {...}]
return str(outcomes)
# good operate 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 operate to construction the outcomes right into a structured string.
You must also make sure the mannequin can return solely a subset of outcomes, as proven with the max_results parameter. This helps the mannequin loads, particularly with features 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 could have a variety of instruments, lots of which is able to solely be related for the agent to make use of at particular steps. If you already know a software isn’t related for an agent at a given time, you need to preserve the software out of the context.
Informative error dealing with
Informative error dealing with is essential when offering brokers with instruments. You should assist the agent perceive what it’s doing flawed. Often, the uncooked error messages offered by Python are bloated and never that straightforward to know.
Beneath is an efficient instance of error dealing with in instruments, the place the agent is instructed what the error was and methods to take care of it. For instance, when encountering charge restrict errors, we inform the agent to particularly sleep earlier than attempting once more. This simplifies the issue loads for the agent, because it doesn’t should cause 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"Price 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 likely to be down, inform the consumer of the difficulty with the inform_user operate."
besides requests.exceptions.HTTPError as e:
return f"HTTP error occurred: {e}. The operate failed with http error. This normally occurs due to entry points. Make sure you validate earlier than utilizing this operate"
besides Exception as e:
return f"An surprising error occurred: {e}"
You must have such error dealing with for all features, holding the next factors in thoughts:
- Error messages must be informative of what occurred
- If you already know the repair (or potential fixes) for a selected error, inform the LLM methods 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 predominant matters: Particular context engineering ideas, shortening the brokers’ context, and methods to present your brokers with instruments. These are all foundational matters you have to perceive to construct a superb AI agent. There are additionally additional matters that you need to study extra about, such because the consideration of pre-computed info or inference-time info retrieval. I’ll cowl this matter in a future article. Agentic context engineering will proceed to be a brilliant related matter, and understanding methods to deal with the context of an agent is, and shall be, elementary to future AI agent developments.
👉 Discover me on socials:
🧑💻 Get in touch
✍️ Medium
It’s also possible to learn a few of my different articles:

