get_token_reduction
Calculate token usage statistics comparing Chain of Draft versus Chain of Thought reasoning approaches to optimize AI model efficiency.
Instructions
Get token reduction statistics for CoD vs CoT
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- server.py:171-187 (handler)Main handler function for the 'get_token_reduction' tool, registered via @app.tool() decorator. Fetches stats from analytics and formats the response string.@app.tool() async def get_token_reduction() -> str: """Get token reduction statistics for CoD vs CoT.""" stats = await analytics.get_token_reduction_stats() result = "Token Reduction Analysis:\n\n" if not stats: return "No reduction data available yet." for stat in stats: result += f"Domain: {stat['domain']}\n" result += f"CoD avg tokens: {stat['cod_avg_tokens']:.1f}\n" result += f"CoT avg tokens: {stat['cot_avg_tokens']:.1f}\n" result += f"Reduction: {stat['reduction_percentage']:.1f}%\n\n" return result
- analytics.py:115-150 (helper)Database query helper that computes token reduction statistics by averaging tokens used for CoD and CoT approaches per domain.async def get_token_reduction_stats(self): """Calculate token reduction statistics for CoD vs CoT.""" session = self.Session() try: domains = session.query(InferenceRecord.domain).distinct().all() results = [] for domain_row in domains: domain = domain_row[0] # Get average tokens for CoD and CoT approaches in this domain cod_avg = session.query(func.avg(InferenceRecord.tokens_used)).filter( InferenceRecord.domain == domain, InferenceRecord.approach == "CoD" ).scalar() or 0 cot_avg = session.query(func.avg(InferenceRecord.tokens_used)).filter( InferenceRecord.domain == domain, InferenceRecord.approach == "CoT" ).scalar() or 0 if cot_avg > 0: reduction_percentage = (1 - (cod_avg / cot_avg)) * 100 else: reduction_percentage = 0 results.append({ "domain": domain, "cod_avg_tokens": cod_avg, "cot_avg_tokens": cot_avg, "reduction_percentage": reduction_percentage }) return results finally: session.close()
- index.js:542-549 (schema)Explicit JSON schema definition for the 'get_token_reduction' tool with no input parameters.const TOKEN_TOOL = { name: "get_token_reduction", description: "Get token reduction statistics for CoD vs CoT", inputSchema: { type: "object", properties: {} } };
- index.js:715-737 (handler)JavaScript handler for 'get_token_reduction' tool in the MCP server request handler, formats stats from analyticsDb.if (name === "get_token_reduction") { const stats = analyticsDb.getTokenReductionStats(); let result = "Token Reduction Analysis:\n\n"; if (!stats || stats.length === 0) { result = "No reduction data available yet."; } else { for (const stat of stats) { result += `Domain: ${stat.domain}\n`; result += `CoD avg tokens: ${stat.cod_avg_tokens.toFixed(1)}\n`; result += `CoT avg tokens: ${stat.cot_avg_tokens.toFixed(1)}\n`; result += `Reduction: ${stat.reduction_percentage.toFixed(1)}%\n\n`; } } return { content: [{ type: "text", text: result }] }; }
- index.js:67-106 (helper)In-memory analytics helper method that calculates token reduction stats by grouping records by domain and averaging CoD/CoT tokens.getTokenReductionStats: function() { // Group by domain const domains = {}; for (const record of this.records) { if (!domains[record.domain]) { domains[record.domain] = { cod_tokens: [], cot_tokens: [] }; } if (record.approach === 'CoD') { domains[record.domain].cod_tokens.push(record.tokens_used); } else if (record.approach === 'CoT') { domains[record.domain].cot_tokens.push(record.tokens_used); } } // Calculate reduction stats return Object.entries(domains).map(([domain, data]) => { const cod_avg = data.cod_tokens.length ? data.cod_tokens.reduce((a, b) => a + b, 0) / data.cod_tokens.length : 0; const cot_avg = data.cot_tokens.length ? data.cot_tokens.reduce((a, b) => a + b, 0) / data.cot_tokens.length : 0; let reduction = 0; if (cot_avg > 0 && cod_avg > 0) { reduction = 100 * (1 - (cod_avg / cot_avg)); } return { domain, cod_avg_tokens: cod_avg, cot_avg_tokens: cot_avg, reduction_percentage: reduction }; }); }