To clear old tool results, set contextStrategy at the top level of the agent’s YAML, next to name and model.
dotnet add package RecursiveAI.OneLlmRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
contextStrategy: type: clearToolResults triggerPercent: 70 keepLast: 1
Unzip the example, then dotnet run.
In the example, the final model request contains [tool result cleared to save context] for the first page and Page 2 for the latest page.
The full program
The whole agent definition and its prompt, “Read the first two pages,” are inline in Program.cs. The tool returns Page 1 and Page 2; there are no files to read. The in-process ScriptedProvider, which ignores the model name gpt-6-luna when choosing its reply, reports synthetic usage to cross the threshold, then the program prints the tool results OneLLM put in the final request. No model or API key is needed.
using OneLlm; using OneLlm.Definitions; using OneLlm.Messages; using OneLlm.Testing; using OneLlm.Tools; var runtime = new Runtime(); var stub = new ScriptedProvider("openai"); runtime.Providers.Set("openai", stub); int nextPage = 0; runtime.Tools.Add(Tool.Create("read_page", () => $"Page {++nextPage}")); stub.Enqueue(ReadPage("first"), ReadPage("second"), new ScriptStep { ToolCalls = [new("done", "task_complete", """ {"result":"Read two pages.","summary":"Done"} """)] }); var agent = AgentDefinition.Parse(""" name: reader model: gpt-6-luna systemPrompt: Read two pages and call task_complete. initialPrompts: - user: [{text: Read the first two pages.}] references: - tools: [read_page, task_complete] contextStrategy: type: clearToolResults triggerPercent: 70 keepLast: 1 """); var result = await runtime.RunAgentAsync(agent); Console.WriteLine("Tool results in the final model request:"); foreach (var message in stub.Requests[^1].Messages) foreach (var call in message.ToolCalls) foreach (var part in call.Result.OfType<TextPart>()) Console.WriteLine(part.Text); Console.WriteLine(result.Text); // Simulate a nearly full context without creating a huge sample. static ScriptStep ReadPage(string id) => new() { ToolCalls = [new(id, "read_page", "{}")], Usage = new Usage { InputTokens = int.MaxValue }, };
Tool results in the final model request: [tool result cleared to save context] Page 2 Read two pages.
If one new tool result is too large to fit, clearing older results won’t fix it: the latest result is still included. Return a smaller result from the tool, for example by paging it or returning selected fields.
Variations
Keep two tool-calling turns intact. Replace the strategy with this configuration; the example then shows both Page 1 and Page 2 in the final request.
contextStrategy: type: clearToolResults triggerPercent: 70 keepLast: 2
For a live run, register a real provider on runtime.Providers in place of the stub, set model to a model that provider serves, and keep the same contextStrategy.
Related recipes
The short answer
Set contextStrategy to clearToolResults when old tool outputs take up space but recent ones still matter. OneLLM checks the previous call’s reported usage between turns. Once usage reaches the trigger, it replaces older tool results with a marker instead of removing the calls or dropping the most recent results.
What is an agent context window?
The context window is the amount of material the model can work with in a call. The conversation is only part of what fills it. The model also has to work with instructions and the tools you expose, and OneLLM’s trigger uses both input and output usage from the previous call. A history that contains a short user question and many long tool responses can thus need trimming even though the user has hardly typed anything.
Set the trigger below the window’s limit so there is room for another turn. triggerPercent uses a fraction of the model’s window; triggerTokens takes an absolute threshold instead. Neither is a hard ceiling on the next request. The check looks backward, so a large new input can still overflow before earlier results have been replaced.
How do you manage conversation context in an AI agent?
Clear old results when you can retrieve them again or don’t need their exact contents. Here keepLast: 1 means one tool-calling turn, not one message or one result. If the latest turn makes several tool calls, those results stay together. The definition spec explains why the marker is used: “Every call keeps a result, so no call is orphaned.” The older result text is replaced in the agent’s instance; don’t use that history as an exact archive of raw tool data.
If earlier user messages are what fill the window, or the agent needs to retain facts from them, consider compact instead. It replaces an older span of messages with a model-generated summary, rather than clearing just the tool outputs. That summary can lose details, so retain any data the app must be able to recover outside the compacted conversation. The runtime also supports providerNative where the provider implements compaction; otherwise it skips that strategy and reports a warning.