Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/agent/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# mycoder-agent

## 0.2.0

### Minor Changes

- Add token caching, better user input handling, token usage logging (--tokenUsage), the ability to see the browser (--headless=false), and log prefixes with emojis.

## 0.1.3

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/agent/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mycoder-agent",
"version": "0.1.4",
"version": "0.2.0",
"description": "Agent module for mycoder - an AI-powered software development assistant",
"type": "module",
"main": "dist/index.js",
Expand Down
11 changes: 6 additions & 5 deletions packages/agent/src/core/executeToolCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ export const executeToolCall = async (
tools: Tool[],
context: ToolContext,
): Promise<string> => {
const logger = new Logger({
name: `Tool:${toolCall.name}`,
parent: context.logger,
});

const tool = tools.find((t) => t.name === toolCall.name);
if (!tool) {
throw new Error(`No tool with the name '${toolCall.name}' exists.`);
}

const logger = new Logger({
name: `Tool:${toolCall.name}`,
parent: context.logger,
customPrefix: tool.logPrefix,
});

const toolContext = {
...context,
logger,
Expand Down
135 changes: 91 additions & 44 deletions packages/agent/src/core/tokens.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,100 @@
import Anthropic from '@anthropic-ai/sdk';

export type TokenUsage = {
input: number;
inputCacheWrites: number;
inputCacheReads: number;
output: number;
};

export const getTokenUsage = (response: Anthropic.Message): TokenUsage => {
return {
input: response.usage.input_tokens,
inputCacheWrites: response.usage.cache_creation_input_tokens ?? 0,
inputCacheReads: response.usage.cache_read_input_tokens ?? 0,
output: response.usage.output_tokens,
};
};

export const addTokenUsage = (a: TokenUsage, b: TokenUsage): TokenUsage => {
return {
input: a.input + b.input,
inputCacheWrites: a.inputCacheWrites + b.inputCacheWrites,
inputCacheReads: a.inputCacheReads + b.inputCacheReads,
output: a.output + b.output,
};
};
import { LogLevel } from '../utils/logger.js';

const PER_MILLION = 1 / 1000000;
const TOKEN_COST: TokenUsage = {
const TOKEN_COST = {
input: 3 * PER_MILLION,
inputCacheWrites: 3.75 * PER_MILLION,
inputCacheReads: 0.3 * PER_MILLION,
cacheWrites: 3.75 * PER_MILLION,
cacheReads: 0.3 * PER_MILLION,
output: 15 * PER_MILLION,
};

const formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 2,
});

export const getTokenCost = (usage: TokenUsage): string => {
return formatter.format(
usage.input * TOKEN_COST.input +
usage.inputCacheWrites * TOKEN_COST.inputCacheWrites +
usage.inputCacheReads * TOKEN_COST.inputCacheReads +
usage.output * TOKEN_COST.output,
);
};
export class TokenUsage {
public input: number = 0;
public cacheWrites: number = 0;
public cacheReads: number = 0;
public output: number = 0;

export const getTokenString = (usage: TokenUsage): string => {
return `input: ${usage.input} input-cache-writes: ${usage.inputCacheWrites} input-cache-reads: ${usage.inputCacheReads} output: ${usage.output} COST: ${getTokenCost(usage)}`;
};
constructor() {}

add(usage: TokenUsage) {
this.input += usage.input;
this.cacheWrites += usage.cacheWrites;
this.cacheReads += usage.cacheReads;
this.output += usage.output;
}

clone() {
const usage = new TokenUsage();
usage.input = this.input;
usage.cacheWrites = this.cacheWrites;
usage.cacheReads = this.cacheReads;
usage.output = this.output;
return usage;
}

static fromMessage(message: Anthropic.Message) {
const usage = new TokenUsage();
usage.input = message.usage.input_tokens;
usage.cacheWrites = message.usage.cache_creation_input_tokens ?? 0;
usage.cacheReads = message.usage.cache_read_input_tokens ?? 0;
usage.output = message.usage.output_tokens;
return usage;
}

static sum(usages: TokenUsage[]) {
const usage = new TokenUsage();
usages.forEach((u) => usage.add(u));
return usage;
}

getCost() {
const formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 2,
});

return formatter.format(
this.input * TOKEN_COST.input +
this.cacheWrites * TOKEN_COST.cacheWrites +
this.cacheReads * TOKEN_COST.cacheReads +
this.output * TOKEN_COST.output,
);
}

toString() {
return `input: ${this.input} cache-writes: ${this.cacheWrites} cache-reads: ${this.cacheReads} output: ${this.output} COST: ${this.getCost()}`;
}
}

export class TokenTracker {
public tokenUsage = new TokenUsage();
public children: TokenTracker[] = [];

constructor(
public readonly name: string = 'unnamed',
public readonly parent: TokenTracker | undefined = undefined,
public readonly logLevel: LogLevel = parent?.logLevel ?? LogLevel.debug,
) {
if (parent) {
parent.children.push(this);
}
}

getTotalUsage() {
const usage = this.tokenUsage.clone();
this.children.forEach((child) => usage.add(child.getTotalUsage()));
return usage;
}

getTotalCost() {
const usage = this.getTotalUsage();
return usage.getCost();
}

toString() {
return `${this.name}: ${this.getTotalUsage().toString()}`;
}
}
102 changes: 0 additions & 102 deletions packages/agent/src/core/toolAgent.cache.test.ts

This file was deleted.

15 changes: 8 additions & 7 deletions packages/agent/src/core/toolAgent.respawn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ import { toolAgent } from '../../src/core/toolAgent.js';
import { getTools } from '../../src/tools/getTools.js';
import { MockLogger } from '../utils/mockLogger.js';

const logger = new MockLogger();
import { TokenTracker } from './tokens.js';

const toolContext = {
logger: new MockLogger(),
headless: true,
workingDirectory: '.',
tokenTracker: new TokenTracker(),
};
// Mock Anthropic SDK
vi.mock('@anthropic-ai/sdk', () => {
return {
Expand Down Expand Up @@ -52,12 +58,7 @@ describe('toolAgent respawn functionality', () => {
temperature: 0,
getSystemPrompt: () => 'test system prompt',
},
{
logger,
headless: true,
workingDirectory: '.',
tokenLevel: 'debug',
},
toolContext,
);

expect(result.result).toBe(
Expand Down
Loading
Loading