Transforming a CLI into an AI-Native CLI
AI / CLI / ESKit / Context Engineering
October 2026 · ESKit experiment
How Much Context Does an Agent Need? Dynamic Context Loading in ESKit AI
At the end of the previous experiment, I had added events and execution tracing to ESKit AI so I could observe what the agent was doing across multi-step workflows.
I ended that post with another question:
How do I give the agent the right context at the right time without sending everything every time?
ESKit's AI command definitions are derived from the same command definitions used by the CLI. This keeps the AI interface connected to the actual ESKit command surface instead of maintaining a separate hand-written tool API.
In an earlier experiment, I had already reduced the command representation from roughly 25K input tokens to around 7K by compiling the command description through a series of transformation passes.
But even after making the representation smaller, another question remained:
Does the agent need all of those command definitions for every request?
From Context Optimization to Context Selection
Consider a simple request:
How do I create an index?
To answer that question, the model does not need detailed definitions for snapshot restoration, archive synchronization, repository management, ILM, and every other ESKit operation. The command definitions may all be valid information, but they are not all relevant information for the current task.
That shifted the experiment from:
How can I make the command context smaller?
to:
How much command context does the agent need at one time?
The add_context Tool
To let the agent request additional command information, I added an
add_context tool.
This is different from a normal ESKit command tool.
A normal tool such as an index, snapshot, or repository operation eventually
executes an ESKit command.
The add_context is an agent-control tool. It changes
what command information is available to the model.
Conceptually, a request can look like this:
User:
How do I create an index?
Agent:
I can see from the overview that ESKit has an index context.
Tool call:
add_context(["index"])
ESKit AI:
Adds "index" to the active contexts.
Recompiles the command description through the passes.
Agent:
Receives the detailed index command definition and continues.
The tool does not execute an Elasticsearch operation. It changes the agent's available command knowledge. The timeline is roughly like this:
This turned dynamic context acquisition into an explicit part of the agent loop rather than something hidden inside prompt construction.
Reusing the Command Compilation Pipeline
In the earlier context optimization work, ESKit already had a pass-based pipeline for compiling the full command definition into the representation provided to the model.
For example, passes could remove fields that were unnecessary for the LLM or deduplicate common arguments. Dynamic context selection generation fit naturally into that same pipeline.
New phase was added to the pipeline to support dynamic context selection.
SelectContextsdetermines which detailed command contexts are currently active and includes them in the output.
The existing cleanup passes can continue processing the result afterward. It is another transformation of the same command definition that ESKit already uses (Command IR), and the output is still a single optimized command description which is used for prompt and generating tool call descriptions for the model.
Dynamic Context Loading In the Agent Flow
With those passes in place, the command description can be rebuilt whenever the set of active contexts changes.
Three Context Strategies
Now the dynamic context loading can be applied to the agent flow, the next step is to experiment how much context the agent should know at the start.
Strategy 1: Common Context
My first approach was to identify command contexts that seemed commonly useful and provide them to the model up front. In this case, the commands were to pull the latest cache and to list resources. The idea was those commands would be used as base operations to monitor resources.
This worked, and it was still better than loading the complete command definition. But a command being commonly useful does not mean it is useful for the current request. As I added more commands to the common set, the optimization started to work against itself. The approach reduced context compared with loading everything, but every definition included in the common context becomes a recurring input cost, even when the model never uses it.
Also, one of the other interesting findings was that the model tended to settle down with the common context without asking more context in some requests such as asking what ESKit can do and characteristics of commands such as which commands are destructive. The model was not asking for more context because it assumed it already had the relevant information. More specific requests such as creating an index or pulling the latest cache, the agent used the add_context tool to acquire the relevant command context.
Strategy 2: Minimal Context
The next experiment went in the opposite direction. What if the model started with almost no ESKit command context? And let the agent to figure out what it needed and request anything without any pre-existing command knowledge.
The initial request could remain very small, and detailed command definitions only needed to be loaded when the relevant capability was already known. But minimal context introduced a different problem: discovery.
Consider broader questions such as:
What can ESKit do?
or:
How do I accomplish something if there isn't a dedicated command?
If the model knows almost nothing about the ESKit command surface, it has very little information from which to reason about those questions.
However, I observed that the agent gave me very generic description of ESkit and ended the conversation with asking me if there are anything specific operations I wanted to perform instead of giving me an overview based on the partial common context it had. I preferred this behavior because it was more honest about what it knew and what it didn't know. It was also more likely to ask for the relevant context when it needed it. Basically the agent asked me much more questions than the common contexts.
Strategy 3: Overview Context
The last approach is to give the agent a lightweight overview of the ESKit command surface.
Instead of containing every argument and option, the overview acts more like a table of contents. It can tell the model that ESKit has capabilities related to indices, snapshots, repositories, archives, ILM, reindexing, host operations, and other areas without providing every detailed command definition. When the model determines that it needs one of those capabilities, it can load the detailed context dynamically. This provides enough information for broad capability discovery while avoiding the recurring cost of loading complete definitions that may never be used.
This approach worked well for capability discovery while maintaining efficient token usage. This behavior seems to be suitable for someone who is not familiar with the ESKit command surface and wants to explore what it can do. But still keep the token usage lower than loading all the context up front.
Another new phase, "ContextOverview Pass" was added to the pipeline to support this strategy.
Measuring the Strategies
To put numbers on the comparison, I ran five prompts through each strategy
using claude-haiku-4-5-20251001. Each prompt was run once per
strategy, so these numbers are indicative rather than statistical. Token
counts are the input tokens reported in the ESKit AI trace. For runs that
called add_context, the figure is the sum of both requests.
Initial request (before any add_context)
| Strategy | Input tokens | vs. Common |
|---|---|---|
| Common | 3,312 | — |
| Minimal | 1,612 | −51% |
| Overview | 2,464 | −26% |
Total input tokens per prompt
| Prompt | Common | Minimal | Overview |
|---|---|---|---|
| What is ESKit? | 3,312 | 1,612 | 2,464 |
| How do I create an index? | 7,647 | 4,642 | 6,283 |
| Which operations are destructive? | 3,313 | 11,491 | 2,465 |
| How do I accomplish X when there isn't a dedicated command? | 3,320 | 1,620 | 2,472 |
| How can I see snapshot cache in eskit? | 3,317 | 5,318 | 5,808 |
| Total | 20,909 | 24,683 | 19,492 |
| add_context calls | 1 | 3 | 2 |
On the initial request, Overview was about 26% smaller than Common, while Minimal was about 51% smaller. That part is expected: Minimal begins with almost no ESKit command knowledge, so it has the smallest starting context. The total token counts tell a more interesting story. In this small experiment, Overview used the fewest total input tokens across the five prompts, followed by Common and then Minimal. However, I don't interpret this as evidence that Minimal is generally the most expensive strategy.
Minimal was actually the least expensive strategy for three of the five
prompts. The result changed substantially for
Which operations are destructive?, where the agent loaded all 13
contexts in a single add_context call. The following request grew
from 1,613 to 9,878 input tokens. That single run accounts for much of
Minimal's higher total in this experiment.
With only one run per prompt, these measurements are not intended to establish which strategy is always the most token-efficient. Instead, they show the different behaviors that each starting strategy can produce and helped me decide which tradeoff I prefer as the current ESKit AI default.
Token counts also say nothing by themselves about answer quality. For
How can I see snapshot cache in eskit?, Minimal loaded the
snapshot context, found no matching command, and concluded there
wasn't one. Common and Overview both reached eskit cat snap.
For What is ESKit?, Overview also produced the broadest capability
description of the three.
Comparing the Context Strategies
The three approaches produced different tradeoffs:
| Strategy | Initial Context | Initial Tokens | Total Tokens (5 prompts) | Broad Questions | Tradeoff |
|---|---|---|---|---|---|
| Common | Selected commonly useful command definitions | 3,312 | 20,909 | Limited | Useful capabilities are available immediately, but unrelated definitions are repeatedly sent to the model. |
| Minimal | Almost no ESKit command information | 1,612 | 24,683 | Limited | Guarantees a small starting context, but subsequent context loading depends heavily on how the agent interprets the request. |
| Overview | Lightweight map of the command surface | 2,464 | 19,492 | Good | Adds some initial context to support discovery while allowing detailed definitions to be loaded only when needed. |
For the current ESKit AI design, I prefer the Overview strategy as the default. It keeps the starting context substantially smaller than Common while preserving enough information for the agent to reason about the broader ESKit command surface.
This choice isn't based on the five-prompt token total alone. The experiment also showed differences in discovery behavior and answer quality. Overview gives up some of Minimal's initial token advantage in exchange for giving the agent a map of what is available.
Minimal is still interesting, especially when the relevant operation is already known. This also suggests that context strategy does not necessarily have to be fixed. An exploratory request might benefit from Overview, while a more specific request with known capabilities could potentially start from Minimal. That is something I can experiment with separately.
Making the Context Level Explicit
During testing, I found that the model sometimes confused the overview with the detailed context. Almost behaving like the common context strategy, it assumed it had enough information to reason about a capability when it only had the overview. I therefore added an explicit context level to the command metadata.
An overview definition can look conceptually like:
{
"program": "eskit snap",
"description": "Snapshot commands",
"metadata": {
"context": "snapshot",
"context_level": "overview"
}
}
After the context is loaded, the model receives the detailed definition:
{
"program": "eskit snap",
"description": "Snapshot commands",
"metadata": {
"context": "snapshot",
"context_level": "loaded"
},
"commands": {
...
}
}
This gives the model an explicit distinction between:
I know this capability exists.
and:
I have enough information to use this capability.
Just-in-Time Command Knowledge
With this structure, a simple request can progressively acquire the command information it needs.
How do I create an index?
can result in:
overview
↓
identify index capability
↓
add_context("index")
↓
recompile command description
↓
load detailed index definition
↓
construct the command
A multi-step request can acquire several contexts over time.
Pull the latest cache and show me available indices.
In one test, the agent progressed roughly like this:
overview
↓
load pull + index
↓
execute pull
↓
discover cat is needed
↓
load cat
↓
execute cat
↓
summarize result
ESKit does not need to know the complete execution path before the workflow starts. The agent can progressively acquire command knowledge as the task develops.
Preloading Context with --contexts
Dynamic discovery is useful, but sometimes I already know which command contexts will be needed.
So, I added a --contexts option so those contexts can be supplied
at the beginning of the interaction.
eskit ai "show me the available indices" --contexts cat
Multiple contexts can also be preloaded:
eskit ai \
"pull the latest cache and show me the available indices" \
--contexts pull index cat
This can avoid an additional add_context round when the relevant
capabilities are already known.
Importantly, the supplied contexts are a starting point rather than a restriction.
I tested this by deliberately supplying the wrong initial context.
For an index-related request, I started the agent with the
snapshot context.
The model recognized that the available context was insufficient, called add_context
for the required context, and continued normally.
That was an important behavior to verify:
--contexts provides preloaded knowledge without disabling
dynamic context acquisition.
This Isn't Only About Tokens
Token usage is easy to measure, and it was one of the reasons I started looking at command context more closely.
But the part I find more interesting now is the boundary itself.
What information should the agent have access to at each stage of a task?
ESKit may have a large amount of information available. That does not mean every piece of that information needs to be exposed to the model immediately.
The dynamic-context design gives the agent a controlled way to acquire more command knowledge when it needs it.
The important abstraction is not that some information is unavailable.
It is that information can be exposed progressively through a defined boundary.
The Focus Moved
Once dynamic command contexts started working, another source of context became much more visible: tool results. In one recent test, I preloaded the correct context and asked the agent to show the available indices.
The first model request was approximately:
3,334 input tokens
The agent then executed the index listing operation.
The next request, which included the raw tool result, grew to approximately:
17,051 input tokens
At that point, command definitions were no longer the dominant source of context. I also noticed something else during the experiment. When the model received a large raw index result, its final summary contained a few small counting and status inconsistencies. More information was not necessarily making the reasoning easier.
How Much Result Does an Agent Need?
This leads naturally to the next experiment.
ESKit already separates raw command results from the projections presented to normal CLI for user friendly rendering. A command can retrieve a large amount of underlying information while the CLI displays only the fields useful for that particular view. Currently, the AI can receive much closer to the raw result.
But if I treat the AI as another ESKit user, perhaps its default view should also be the projected result.
The goal is not necessarily to hide information from the model or simply save tokens.
It is another information capability boundary: Providing the agent with the representation appropriate for the current task, while retaining a path to more detail when needed.
Recap
The progression of the series now looks like this:
For this part, I started by asking:
How much command context does an agent need?
The next question is:
How much result data does an agent need?
A Side Note: Dynamic Context and Prompt Caching
There is another tradeoff in the current design that I haven't measured yet: prompt caching.
The current add_context implementation recompiles the command
description whenever the active contexts change. Because that description
contributes to the prompt and tool definitions sent to the model, changing it
may reduce how much of the previous request can benefit from prompt caching.
This creates an interesting tradeoff. Dynamic context loading reduces the amount of command information sent to the model at a given time, but changing that information between turns may work against prefix-based caching. I have not measured that interaction yet, so for now I treat it as a question for a separate experiment rather than a conclusion from the current one.
This may also matter more in longer multi-step workflows than in the short experiments in this post. Several of the initial requests were relatively small, while later turns can grow substantially once tool results and conversation history accumulate.
A Future Experiment: A Cache-Friendly Mode
One alternative would be to keep the system prompt and tool definitions
stable and return newly requested command definitions as the result of
add_context. The loaded definition could still carry the
"context_level": "loaded" marker so the model can distinguish
between:
I know this capability exists.
and:
I have enough information to use this capability.
In that design, new command knowledge would accumulate in the conversation rather than causing the command description itself to be rebuilt.
My hypothesis is that this could make better use of prompt caching in longer sessions, potentially reducing cost or latency. The tradeoff is that loaded definitions would accumulate in the conversation history, and the execution interface might need to become more generic.
I haven't verified whether that design is actually better. A future experiment could use the tracing added in Part 6 to record cache read and write token counts and compare the two approaches by cost, latency, and context growth, rather than looking only at raw input tokens.
ESKit Command Implementation →
This is part of an ongoing series of experiments exploring AI-assisted CLI interfaces and agent architectures in ESKit.