Skip to content

Commit e1b1836

Browse files
committed
improved token-caching
1 parent b6c997a commit e1b1836

File tree

5 files changed

+30
-12
lines changed

5 files changed

+30
-12
lines changed

packages/agent/src/core/tokens.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,6 @@ export class TokenTracker {
9595
}
9696

9797
toString() {
98-
return `${this.name}: ${this.tokenUsage.toString()}`;
98+
return `${this.name}: ${this.getTotalUsage().toString()}`;
9999
}
100100
}

packages/agent/src/core/toolAgent.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,15 @@ function addCacheControlToContentBlocks(
199199
): ContentBlockParam[] {
200200
return content.map((c, i) => {
201201
if (i === content.length - 1) {
202-
if (c.type === 'text' || c.type === 'document' || c.type === 'image') {
202+
if (
203+
c.type === 'text' ||
204+
c.type === 'document' ||
205+
c.type === 'image' ||
206+
c.type === 'tool_use' ||
207+
c.type === 'tool_result' ||
208+
c.type === 'thinking' ||
209+
c.type === 'redacted_thinking'
210+
) {
203211
return { ...c, cache_control: { type: 'ephemeral' } };
204212
}
205213
}
@@ -209,7 +217,7 @@ function addCacheControlToContentBlocks(
209217
function addCacheControlToMessages(
210218
messages: Anthropic.Messages.MessageParam[],
211219
): Anthropic.Messages.MessageParam[] {
212-
return messages.map((m) => {
220+
return messages.map((m, i) => {
213221
if (typeof m.content === 'string') {
214222
return {
215223
...m,
@@ -224,7 +232,10 @@ function addCacheControlToMessages(
224232
}
225233
return {
226234
...m,
227-
content: addCacheControlToContentBlocks(m.content),
235+
content:
236+
i >= messages.length - 2
237+
? addCacheControlToContentBlocks(m.content)
238+
: m.content,
228239
};
229240
});
230241
}

packages/agent/src/tools/system/shellStart.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ export const shellStartTool: Tool<Parameters, ReturnType> = {
189189
{ logger },
190190
) => {
191191
logger.info(
192-
`🖥️ Starting "${command}", ${description} (timeout: ${timeout}ms)`,
192+
`Starting "${command}", ${description} (timeout: ${timeout}ms)`,
193193
);
194194
},
195195
logReturns: (output, { logger }) => {

packages/cli/src/commands/$default.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as fs from 'fs/promises';
22
import { createInterface } from 'readline/promises';
33

4+
import chalk from 'chalk';
45
import {
56
toolAgent,
67
Logger,
@@ -107,6 +108,15 @@ export const command: CommandModule<object, DefaultArgs> = {
107108

108109
const tools = getTools();
109110

111+
// Error handling
112+
process.on('SIGINT', () => {
113+
logger.log(
114+
tokenTracker.logLevel,
115+
chalk.blueBright(`[Token Usage Total] ${tokenTracker.toString()}`),
116+
);
117+
process.exit(0);
118+
});
119+
110120
const result = await toolAgent(prompt, tools, undefined, {
111121
logger,
112122
headless: argv.headless ?? true,
@@ -123,6 +133,9 @@ export const command: CommandModule<object, DefaultArgs> = {
123133
logger.error('An error occurred:', error);
124134
}
125135

126-
logger.info(`Token Usage: ${tokenTracker.toString()}`);
136+
logger.log(
137+
tokenTracker.logLevel,
138+
chalk.blueBright(`[Token Usage Total] ${tokenTracker.toString()}`),
139+
);
127140
},
128141
};

packages/cli/src/index.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@ const main = async () => {
3535
console.log();
3636
}
3737

38-
// Error handling
39-
process.on('SIGINT', () => {
40-
logger.warn('\nGracefully shutting down...');
41-
process.exit(0);
42-
});
43-
4438
process.on('uncaughtException', (error) => {
4539
logger.error(
4640
'Fatal error:',

0 commit comments

Comments
 (0)