State on the AgentContext, not the Tool

public class PaginatingTool implements Tool {
  public Object call(Map args) {
    var ctx = ToolContext.current();
    var cursor = ctx.getSessionAttribute("my_cursor", String.class).orElse(null);
    var result = downstream.fetch(cursor);
    ctx.setSessionAttribute("my_cursor", result.nextCursor());
    return result.data();
  }
}
Advertisement

Never on instance fields

Tool instances are shared across sessions. Instance fields = data leak. Use context.

Advertisement

Explicit lifecycle

Set state early, read it, update it, drop it on session end. Nothing lingers past.