add usage info to branch summary, compaction and tool result entries (#6671)

* add usage info to branch summary entries

* add usage to compaction entries

* allow custom tools to report llm usage in tool results

* allow observing and patching usage in tool_result hooks

* agent-harness: save usage in entries

for compaction, branch summaries and tool results
This commit is contained in:
David Brailovsky
2026-07-20 16:41:43 +02:00
committed by GitHub
parent c179395218
commit 2fd3868401
29 changed files with 844 additions and 122 deletions
+76 -12
View File
@@ -21,20 +21,46 @@ function createSession(options: {
reasoning?: boolean;
thinkingLevel?: string;
usage?: AssistantUsage;
branchUsage?: AssistantUsage;
compactionUsage?: AssistantUsage;
toolUsage?: AssistantUsage;
}): AgentSession {
const usage = options.usage;
const entries =
usage === undefined
? []
: [
{
type: "message",
message: {
role: "assistant",
usage,
},
},
];
const entries: Array<Record<string, unknown>> = [];
if (usage !== undefined) {
entries.push({
type: "message",
message: {
role: "assistant",
usage,
},
});
}
if (options.branchUsage !== undefined) {
entries.push({
type: "branch_summary",
usage: options.branchUsage,
});
}
if (options.compactionUsage !== undefined) {
entries.push({
type: "compaction",
usage: options.compactionUsage,
});
}
if (options.toolUsage !== undefined) {
entries.push({
type: "message",
message: {
role: "toolResult",
usage: options.toolUsage,
},
});
}
const session = {
state: {
@@ -125,6 +151,44 @@ describe("FooterComponent width handling", () => {
}
});
it("includes summary and tool result usage in the total cost", () => {
const session = createSession({
sessionName: "",
usage: {
input: 100,
output: 10,
cacheRead: 0,
cacheWrite: 0,
cost: { total: 0.5 },
},
branchUsage: {
input: 20,
output: 5,
cacheRead: 0,
cacheWrite: 0,
cost: { total: 0.25 },
},
compactionUsage: {
input: 5,
output: 2,
cacheRead: 0,
cacheWrite: 0,
cost: { total: 0.125 },
},
toolUsage: {
input: 15,
output: 3,
cacheRead: 0,
cacheWrite: 0,
cost: { total: 0.375 },
},
});
const footer = new FooterComponent(session, createFooterData(1));
const statsLine = stripAnsi(footer.render(120)[1]);
expect(statsLine).toContain("$1.250");
});
it("shows the latest cache hit rate when cache usage is present", () => {
const session = createSession({
sessionName: "",