-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy path23-token-tracking.ts
More file actions
84 lines (75 loc) · 2.75 KB
/
Copy path23-token-tracking.ts
File metadata and controls
84 lines (75 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* Token & Cost Tracking -- monitor LLM token usage per agent run.
*
* Demonstrates the `tokenUsage` field on `AgentResult` which provides
* aggregated token usage across all LLM calls in an agent execution.
*
* Requirements:
* - Conductor server with LLM support
* - CONDUCTOR_SERVER_URL=http://localhost:8080/api
* - CONDUCTOR_AGENT_LLM_MODEL=openai/gpt-4o-mini
*/
import { Agent, AgentRuntime, tool } from '@io-orkes/conductor-javascript/agents';
import { llmModel } from './settings';
const calculate = tool(
async (args: { expression: string }) => {
// For demo only -- use a safe evaluator in production
const result = new Function('return ' + args.expression)();
return String(result);
},
{
name: 'calculate',
description: 'Evaluate a mathematical expression.',
inputSchema: {
type: 'object',
properties: {
expression: { type: 'string', description: 'The mathematical expression to evaluate' },
},
required: ['expression'],
},
},
);
export const agent = new Agent({
name: 'math_tutor',
model: llmModel,
tools: [calculate],
instructions:
'You are a math tutor. Solve problems step by step, using the calculate ' +
'tool for computations. Explain each step clearly.',
});
// -- Run -------------------------------------------------------------------
async function main() {
const runtime = new AgentRuntime();
try {
const result = await runtime.run(
agent,
'Calculate the compound interest on $10,000 at 5% annual rate ' +
'compounded monthly for 3 years.',
);
result.printResult();
// Token usage is automatically extracted from the workflow
if (result.tokenUsage) {
console.log('Token Usage Summary:');
console.log(` Prompt tokens: ${result.tokenUsage.promptTokens}`);
console.log(` Completion tokens: ${result.tokenUsage.completionTokens}`);
console.log(` Total tokens: ${result.tokenUsage.totalTokens}`);
// Estimate cost (example pricing -- adjust for your model)
const promptCost = result.tokenUsage.promptTokens * 0.0025 / 1000;
const completionCost = result.tokenUsage.completionTokens * 0.01 / 1000;
console.log(`\n Estimated cost: $${(promptCost + completionCost).toFixed(4)}`);
} else {
console.log('(Token usage not available from workflow)');
}
// Production pattern:
// 1. Deploy once during CI/CD (optional -- serve() below also deploys):
// await runtime.deploy(agent);
// CLI alternative:
// conductor deploy --package examples/agents --agents math_tutor
//
// 2. In a separate long-lived worker process (deploys + registers workers + starts polling):
// await runtime.serve(agent);
} finally {
await runtime.shutdown();
}
}
main().catch(console.error);