claude-flow-novice 2.14.22 → 2.14.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. package/{claude-assets/agents → .claude/cfn-agents-ignore}/cfn-seo-team/cfn-seo-coordinator.md +410 -414
  2. package/{claude-assets/agents → .claude/cfn-agents-ignore}/cfn-seo-team/competitive-seo-analyst.md +420 -423
  3. package/{claude-assets/agents → .claude/cfn-agents-ignore}/cfn-seo-team/content-atomization-specialist.md +577 -580
  4. package/{claude-assets/agents → .claude/cfn-agents-ignore}/cfn-seo-team/content-seo-strategist.md +242 -245
  5. package/{claude-assets/agents → .claude/cfn-agents-ignore}/cfn-seo-team/eeat-content-auditor.md +386 -389
  6. package/{claude-assets/agents → .claude/cfn-agents-ignore}/cfn-seo-team/geo-optimization-expert.md +266 -269
  7. package/{claude-assets/agents → .claude/cfn-agents-ignore}/cfn-seo-team/link-building-specialist.md +288 -291
  8. package/{claude-assets/agents → .claude/cfn-agents-ignore}/cfn-seo-team/local-seo-optimizer.md +330 -333
  9. package/{claude-assets/agents → .claude/cfn-agents-ignore}/cfn-seo-team/programmatic-seo-engineer.md +241 -244
  10. package/{claude-assets/agents → .claude/cfn-agents-ignore}/cfn-seo-team/schema-markup-engineer.md +427 -430
  11. package/{claude-assets/agents → .claude/cfn-agents-ignore}/cfn-seo-team/seo-analytics-specialist.md +373 -376
  12. package/{claude-assets/agents → .claude/cfn-agents-ignore}/cfn-seo-team/seo-validators/accessibility-validator.md +561 -565
  13. package/{claude-assets/agents → .claude/cfn-agents-ignore}/cfn-seo-team/seo-validators/audience-validator.md +480 -484
  14. package/{claude-assets/agents → .claude/cfn-agents-ignore}/cfn-seo-team/seo-validators/branding-validator.md +448 -452
  15. package/{claude-assets/agents → .claude/cfn-agents-ignore}/cfn-seo-team/seo-validators/humanizer-validator.md +329 -333
  16. package/{claude-assets/agents → .claude/cfn-agents-ignore}/cfn-seo-team/technical-seo-specialist.md +227 -231
  17. package/claude-assets/agents/cfn-dev-team/CLAUDE.md +9 -29
  18. package/claude-assets/agents/cfn-dev-team/analysts/root-cause-analyst.md +1 -4
  19. package/claude-assets/agents/cfn-dev-team/architecture/goal-planner.md +1 -4
  20. package/claude-assets/agents/cfn-dev-team/architecture/planner.md +1 -4
  21. package/claude-assets/agents/cfn-dev-team/architecture/system-architect.md +1 -4
  22. package/claude-assets/agents/cfn-dev-team/coordinators/cfn-frontend-coordinator.md +536 -540
  23. package/claude-assets/agents/cfn-dev-team/coordinators/cfn-v3-coordinator.md +1 -4
  24. package/claude-assets/agents/cfn-dev-team/coordinators/epic-creator.md +1 -5
  25. package/claude-assets/agents/cfn-dev-team/coordinators/multi-sprint-coordinator.md +1 -3
  26. package/claude-assets/agents/cfn-dev-team/dev-ops/devops-engineer.md +1 -5
  27. package/claude-assets/agents/cfn-dev-team/dev-ops/docker-specialist.md +688 -692
  28. package/claude-assets/agents/cfn-dev-team/dev-ops/github-commit-agent.md +113 -117
  29. package/claude-assets/agents/cfn-dev-team/dev-ops/kubernetes-specialist.md +536 -540
  30. package/claude-assets/agents/cfn-dev-team/dev-ops/monitoring-specialist.md +735 -739
  31. package/claude-assets/agents/cfn-dev-team/developers/api-gateway-specialist.md +901 -905
  32. package/claude-assets/agents/cfn-dev-team/developers/backend-developer.md +1 -4
  33. package/claude-assets/agents/cfn-dev-team/developers/data/data-engineer.md +581 -585
  34. package/claude-assets/agents/cfn-dev-team/developers/database/database-architect.md +272 -276
  35. package/claude-assets/agents/cfn-dev-team/developers/frontend/react-frontend-engineer.md +1 -4
  36. package/claude-assets/agents/cfn-dev-team/developers/frontend/typescript-specialist.md +322 -325
  37. package/claude-assets/agents/cfn-dev-team/developers/frontend/ui-designer.md +1 -5
  38. package/claude-assets/agents/cfn-dev-team/developers/graphql-specialist.md +611 -615
  39. package/claude-assets/agents/cfn-dev-team/developers/rust-developer.md +1 -4
  40. package/claude-assets/agents/cfn-dev-team/documentation/pseudocode.md +1 -4
  41. package/claude-assets/agents/cfn-dev-team/documentation/specification-agent.md +1 -4
  42. package/claude-assets/agents/cfn-dev-team/product-owners/accessibility-advocate-persona.md +105 -108
  43. package/claude-assets/agents/cfn-dev-team/product-owners/cto-agent.md +1 -5
  44. package/claude-assets/agents/cfn-dev-team/product-owners/power-user-persona.md +176 -180
  45. package/claude-assets/agents/cfn-dev-team/reviewers/quality/code-quality-validator.md +53 -30
  46. package/claude-assets/agents/cfn-dev-team/reviewers/quality/cyclomatic-complexity-reducer.md +375 -321
  47. package/claude-assets/agents/cfn-dev-team/reviewers/quality/perf-analyzer.md +52 -30
  48. package/claude-assets/agents/cfn-dev-team/reviewers/quality/security-specialist.md +51 -35
  49. package/claude-assets/agents/cfn-dev-team/testers/api-testing-specialist.md +703 -707
  50. package/claude-assets/agents/cfn-dev-team/testers/chaos-engineering-specialist.md +897 -901
  51. package/claude-assets/agents/cfn-dev-team/testers/e2e/playwright-tester.md +1 -5
  52. package/claude-assets/agents/cfn-dev-team/testers/interaction-tester.md +1 -5
  53. package/claude-assets/agents/cfn-dev-team/testers/load-testing-specialist.md +465 -469
  54. package/claude-assets/agents/cfn-dev-team/testers/playwright-tester.md +1 -4
  55. package/claude-assets/agents/cfn-dev-team/testers/tester.md +1 -4
  56. package/claude-assets/agents/cfn-dev-team/testers/unit/tdd-london-unit-swarm.md +1 -5
  57. package/claude-assets/agents/cfn-dev-team/testers/validation/validation-production-validator.md +1 -3
  58. package/claude-assets/agents/cfn-dev-team/testing/test-validation-agent.md +309 -312
  59. package/claude-assets/agents/cfn-dev-team/utility/agent-builder.md +529 -550
  60. package/claude-assets/agents/cfn-dev-team/utility/analyst.md +1 -4
  61. package/claude-assets/agents/cfn-dev-team/utility/claude-code-expert.md +1040 -1043
  62. package/claude-assets/agents/cfn-dev-team/utility/context-curator.md +86 -89
  63. package/claude-assets/agents/cfn-dev-team/utility/memory-leak-specialist.md +753 -757
  64. package/claude-assets/agents/cfn-dev-team/utility/researcher.md +1 -6
  65. package/claude-assets/agents/cfn-dev-team/utility/z-ai-specialist.md +626 -630
  66. package/claude-assets/agents/custom/cfn-system-expert.md +258 -261
  67. package/claude-assets/agents/custom/claude-code-expert.md +141 -144
  68. package/claude-assets/agents/custom/test-mcp-access.md +24 -26
  69. package/claude-assets/agents/project-only-agents/npm-package-specialist.md +343 -347
  70. package/claude-assets/cfn-agents-ignore/cfn-seo-team/AGENT_CREATION_REPORT.md +481 -0
  71. package/claude-assets/cfn-agents-ignore/cfn-seo-team/DELEGATION_MATRIX.md +371 -0
  72. package/claude-assets/cfn-agents-ignore/cfn-seo-team/HUMANIZER_PROMPTS.md +536 -0
  73. package/claude-assets/cfn-agents-ignore/cfn-seo-team/INTEGRATION_REQUIREMENTS.md +642 -0
  74. package/claude-assets/cfn-agents-ignore/cfn-seo-team/cfn-seo-coordinator.md +410 -0
  75. package/claude-assets/cfn-agents-ignore/cfn-seo-team/competitive-seo-analyst.md +420 -0
  76. package/claude-assets/cfn-agents-ignore/cfn-seo-team/content-atomization-specialist.md +577 -0
  77. package/claude-assets/cfn-agents-ignore/cfn-seo-team/content-seo-strategist.md +242 -0
  78. package/claude-assets/cfn-agents-ignore/cfn-seo-team/eeat-content-auditor.md +386 -0
  79. package/claude-assets/cfn-agents-ignore/cfn-seo-team/geo-optimization-expert.md +266 -0
  80. package/claude-assets/cfn-agents-ignore/cfn-seo-team/link-building-specialist.md +288 -0
  81. package/claude-assets/cfn-agents-ignore/cfn-seo-team/local-seo-optimizer.md +330 -0
  82. package/claude-assets/cfn-agents-ignore/cfn-seo-team/programmatic-seo-engineer.md +241 -0
  83. package/claude-assets/cfn-agents-ignore/cfn-seo-team/schema-markup-engineer.md +427 -0
  84. package/claude-assets/cfn-agents-ignore/cfn-seo-team/seo-analytics-specialist.md +373 -0
  85. package/claude-assets/cfn-agents-ignore/cfn-seo-team/seo-validators/accessibility-validator.md +561 -0
  86. package/claude-assets/cfn-agents-ignore/cfn-seo-team/seo-validators/audience-validator.md +480 -0
  87. package/claude-assets/cfn-agents-ignore/cfn-seo-team/seo-validators/branding-validator.md +448 -0
  88. package/claude-assets/cfn-agents-ignore/cfn-seo-team/seo-validators/humanizer-validator.md +329 -0
  89. package/claude-assets/cfn-agents-ignore/cfn-seo-team/technical-seo-specialist.md +227 -0
  90. package/dist/agents/agent-loader.js +467 -133
  91. package/dist/agents/agent-loader.js.map +1 -1
  92. package/dist/cli/config-manager.js +91 -109
  93. package/dist/cli/config-manager.js.map +1 -1
  94. package/package.json +2 -2
  95. /package/{claude-assets/agents → .claude/cfn-agents-ignore}/cfn-seo-team/AGENT_CREATION_REPORT.md +0 -0
  96. /package/{claude-assets/agents → .claude/cfn-agents-ignore}/cfn-seo-team/DELEGATION_MATRIX.md +0 -0
  97. /package/{claude-assets/agents → .claude/cfn-agents-ignore}/cfn-seo-team/HUMANIZER_PROMPTS.md +0 -0
  98. /package/{claude-assets/agents → .claude/cfn-agents-ignore}/cfn-seo-team/INTEGRATION_REQUIREMENTS.md +0 -0
@@ -1,630 +1,626 @@
1
- ---
2
- name: z-ai-specialist
3
- description: |
4
- MUST BE USED for Z.ai platform optimization, custom API routing, cost savings analysis, and provider integration.
5
- Use PROACTIVELY for Z.ai setup, routing configuration, cost analysis, API provider switching, usage monitoring.
6
- ALWAYS delegate for "Z.ai integration", "custom routing", "cost optimization", "API provider", "routing rules".
7
- Keywords - Z.ai, custom routing, API gateway, cost savings, provider switching, usage monitoring, routing rules
8
- tools: [Read, Write, Edit, Bash, Grep, Glob, TodoWrite]
9
- model: sonnet
10
- type: specialist
11
- capabilities:
12
- - zai-platform-integration
13
- - custom-routing-config
14
- - cost-analysis
15
- - provider-switching
16
- - usage-monitoring
17
- - routing-optimization
18
- acl_level: 1
19
- validation_hooks:
20
- - agent-template-validator
21
- - test-coverage-validator
22
- lifecycle:
23
- pre_task: |
24
- sqlite-cli exec "INSERT INTO agents (id, type, status, spawned_at) VALUES ('${AGENT_ID}', 'z-ai-specialist', 'active', CURRENT_TIMESTAMP)"
25
- post_task: |
26
- sqlite-cli exec "UPDATE agents SET status = 'completed', confidence = ${CONFIDENCE_SCORE}, completed_at = CURRENT_TIMESTAMP WHERE id = '${AGENT_ID}'"
27
- ---
28
-
29
- # Z.ai Specialist Agent
30
-
31
- ## Core Responsibilities
32
- - Configure and optimize Z.ai custom routing
33
- - Implement cost-effective API provider switching
34
- - Analyze usage patterns and cost savings
35
- - Set up routing rules for CLI-spawned agents
36
- - Monitor API usage and performance metrics
37
- - Configure fallback and failover strategies
38
- - Implement A/B testing for different providers
39
- - Establish cost optimization recommendations
40
-
41
- ## Technical Expertise
42
-
43
- ### Z.ai Platform Overview
44
-
45
- Z.ai provides cost-optimized AI model routing with:
46
- - **95-98% cost savings** vs direct Anthropic API
47
- - **Custom routing** for CLI-spawned agents
48
- - **Provider switching** (Anthropic, OpenAI, etc.)
49
- - **Usage analytics** and monitoring
50
- - **Automatic failover** and fallback
51
-
52
- ### Custom Routing Configuration
53
-
54
- #### Enable Z.ai in Claude Flow Novice
55
- ```bash
56
- #!/bin/bash
57
- # enable-zai-routing.sh
58
-
59
- echo "Enabling Z.ai custom routing for Claude Flow Novice..."
60
-
61
- # Step 1: Activate custom routing
62
- /custom-routing-activate
63
-
64
- # Step 2: Verify configuration
65
- if grep -q "CUSTOM_ROUTING_ENABLED=true" ~/.claude/config; then
66
- echo "✅ Custom routing enabled"
67
- else
68
- echo "❌ Custom routing not enabled"
69
- exit 1
70
- fi
71
-
72
- # Step 3: Check API provider
73
- PROVIDER=$(grep "API_PROVIDER" ~/.claude/config | cut -d'=' -f2)
74
- echo "Current provider: $PROVIDER"
75
-
76
- # Step 4: Verify Z.ai endpoint
77
- if grep -q "zai.us" ~/.claude/config; then
78
- echo "✅ Z.ai endpoint configured"
79
- else
80
- echo "⚠️ Z.ai endpoint not found - verify setup"
81
- fi
82
-
83
- echo "
84
- Custom routing active!
85
-
86
- Cost Savings:
87
- - CLI agents: Use Z.ai routing (~$0.50/1M tokens)
88
- - Task() agents: Use Main Chat provider (Anthropic)
89
- - Combined savings: 95-98% for CLI spawning workflows
90
- "
91
- ```
92
-
93
- #### Routing Configuration File
94
- ```json
95
- {
96
- "routing": {
97
- "version": "2.0",
98
- "enabled": true,
99
- "default_provider": "zai",
100
-
101
- "providers": {
102
- "zai": {
103
- "endpoint": "https://api.zai.us/v1",
104
- "model_mapping": {
105
- "claude-3-5-sonnet": "anthropic/claude-3-5-sonnet",
106
- "claude-3-opus": "anthropic/claude-3-opus",
107
- "claude-3-haiku": "anthropic/claude-3-haiku"
108
- },
109
- "cost_per_1m_tokens": {
110
- "input": 0.50,
111
- "output": 0.50
112
- },
113
- "timeout": 60000,
114
- "retry": {
115
- "max_attempts": 3,
116
- "backoff": "exponential"
117
- }
118
- },
119
-
120
- "anthropic": {
121
- "endpoint": "https://api.anthropic.com/v1",
122
- "model_mapping": {
123
- "claude-3-5-sonnet": "claude-3-5-sonnet-20241022",
124
- "claude-3-opus": "claude-3-opus-20240229",
125
- "claude-3-haiku": "claude-3-haiku-20240307"
126
- },
127
- "cost_per_1m_tokens": {
128
- "claude-3-5-sonnet": { "input": 3.00, "output": 15.00 },
129
- "claude-3-opus": { "input": 15.00, "output": 75.00 },
130
- "claude-3-haiku": { "input": 0.25, "output": 1.25 }
131
- },
132
- "timeout": 60000
133
- }
134
- },
135
-
136
- "routing_rules": [
137
- {
138
- "name": "cli-spawned-agents",
139
- "description": "Route all CLI-spawned agents to Z.ai",
140
- "condition": {
141
- "spawn_method": "cli"
142
- },
143
- "provider": "zai",
144
- "priority": 10
145
- },
146
- {
147
- "name": "task-tool-agents",
148
- "description": "Task() agents use Main Chat provider",
149
- "condition": {
150
- "spawn_method": "task"
151
- },
152
- "provider": "anthropic",
153
- "priority": 5
154
- },
155
- {
156
- "name": "high-priority-fallback",
157
- "description": "Critical tasks fallback to Anthropic on Z.ai failure",
158
- "condition": {
159
- "priority": "critical",
160
- "provider_failed": "zai"
161
- },
162
- "provider": "anthropic",
163
- "priority": 100
164
- }
165
- ],
166
-
167
- "cost_tracking": {
168
- "enabled": true,
169
- "log_file": "~/.claude/usage/cost-log.json",
170
- "aggregate_by": ["day", "provider", "model", "agent_type"],
171
- "alerts": {
172
- "daily_threshold": 50.00,
173
- "monthly_threshold": 1000.00
174
- }
175
- }
176
- }
177
- }
178
- ```
179
-
180
- ### Cost Analysis and Monitoring
181
-
182
- #### Usage Tracking Script
183
- ```javascript
184
- // zai-usage-tracker.js
185
- const fs = require('fs');
186
- const path = require('path');
187
-
188
- class ZaiUsageTracker {
189
- constructor(logPath = '~/.claude/usage/cost-log.json') {
190
- this.logPath = path.resolve(logPath.replace('~', process.env.HOME));
191
- this.ensureLogFile();
192
- }
193
-
194
- ensureLogFile() {
195
- const dir = path.dirname(this.logPath);
196
- if (!fs.existsSync(dir)) {
197
- fs.mkdirSync(dir, { recursive: true });
198
- }
199
- if (!fs.existsSync(this.logPath)) {
200
- fs.writeFileSync(this.logPath, JSON.stringify({ requests: [] }, null, 2));
201
- }
202
- }
203
-
204
- logRequest(request) {
205
- const log = JSON.parse(fs.readFileSync(this.logPath, 'utf8'));
206
-
207
- const entry = {
208
- timestamp: new Date().toISOString(),
209
- provider: request.provider,
210
- model: request.model,
211
- spawn_method: request.spawn_method,
212
- agent_type: request.agent_type,
213
- tokens: {
214
- input: request.input_tokens,
215
- output: request.output_tokens,
216
- total: request.input_tokens + request.output_tokens
217
- },
218
- cost: this.calculateCost(request),
219
- duration_ms: request.duration_ms
220
- };
221
-
222
- log.requests.push(entry);
223
- fs.writeFileSync(this.logPath, JSON.stringify(log, null, 2));
224
-
225
- return entry;
226
- }
227
-
228
- calculateCost(request) {
229
- const rates = {
230
- zai: { input: 0.50, output: 0.50 },
231
- anthropic: {
232
- 'claude-3-5-sonnet': { input: 3.00, output: 15.00 },
233
- 'claude-3-opus': { input: 15.00, output: 75.00 },
234
- 'claude-3-haiku': { input: 0.25, output: 1.25 }
235
- }
236
- };
237
-
238
- const rate = request.provider === 'zai'
239
- ? rates.zai
240
- : rates.anthropic[request.model] || rates.anthropic['claude-3-5-sonnet'];
241
-
242
- const inputCost = (request.input_tokens / 1000000) * rate.input;
243
- const outputCost = (request.output_tokens / 1000000) * rate.output;
244
-
245
- return {
246
- input: inputCost,
247
- output: outputCost,
248
- total: inputCost + outputCost,
249
- currency: 'USD'
250
- };
251
- }
252
-
253
- getCostSummary(startDate, endDate) {
254
- const log = JSON.parse(fs.readFileSync(this.logPath, 'utf8'));
255
-
256
- const filtered = log.requests.filter(r => {
257
- const ts = new Date(r.timestamp);
258
- return ts >= startDate && ts <= endDate;
259
- });
260
-
261
- const summary = {
262
- total_requests: filtered.length,
263
- total_tokens: 0,
264
- total_cost: 0,
265
- by_provider: {},
266
- by_model: {},
267
- by_spawn_method: {}
268
- };
269
-
270
- filtered.forEach(request => {
271
- summary.total_tokens += request.tokens.total;
272
- summary.total_cost += request.cost.total;
273
-
274
- // By provider
275
- if (!summary.by_provider[request.provider]) {
276
- summary.by_provider[request.provider] = {
277
- requests: 0,
278
- tokens: 0,
279
- cost: 0
280
- };
281
- }
282
- summary.by_provider[request.provider].requests++;
283
- summary.by_provider[request.provider].tokens += request.tokens.total;
284
- summary.by_provider[request.provider].cost += request.cost.total;
285
-
286
- // By model
287
- if (!summary.by_model[request.model]) {
288
- summary.by_model[request.model] = {
289
- requests: 0,
290
- tokens: 0,
291
- cost: 0
292
- };
293
- }
294
- summary.by_model[request.model].requests++;
295
- summary.by_model[request.model].tokens += request.tokens.total;
296
- summary.by_model[request.model].cost += request.cost.total;
297
-
298
- // By spawn method
299
- if (!summary.by_spawn_method[request.spawn_method]) {
300
- summary.by_spawn_method[request.spawn_method] = {
301
- requests: 0,
302
- tokens: 0,
303
- cost: 0
304
- };
305
- }
306
- summary.by_spawn_method[request.spawn_method].requests++;
307
- summary.by_spawn_method[request.spawn_method].tokens += request.tokens.total;
308
- summary.by_spawn_method[request.spawn_method].cost += request.cost.total;
309
- });
310
-
311
- return summary;
312
- }
313
-
314
- calculateSavings() {
315
- const today = new Date();
316
- const monthStart = new Date(today.getFullYear(), today.getMonth(), 1);
317
-
318
- const summary = this.getCostSummary(monthStart, today);
319
-
320
- // Calculate what cost would have been with only Anthropic
321
- const zaiUsage = summary.by_provider.zai || { tokens: 0, cost: 0 };
322
- const anthropicRate = 3.00; // Average Anthropic rate (input)
323
-
324
- const hypotheticalCost = (zaiUsage.tokens / 1000000) * anthropicRate;
325
- const actualCost = zaiUsage.cost;
326
- const savings = hypotheticalCost - actualCost;
327
- const savingsPercent = (savings / hypotheticalCost) * 100;
328
-
329
- return {
330
- actual_cost: actualCost.toFixed(2),
331
- hypothetical_cost: hypotheticalCost.toFixed(2),
332
- savings: savings.toFixed(2),
333
- savings_percent: savingsPercent.toFixed(2),
334
- tokens_via_zai: zaiUsage.tokens,
335
- requests_via_zai: zaiUsage.requests
336
- };
337
- }
338
-
339
- generateReport() {
340
- const today = new Date();
341
- const monthStart = new Date(today.getFullYear(), today.getMonth(), 1);
342
-
343
- const summary = this.getCostSummary(monthStart, today);
344
- const savings = this.calculateSavings();
345
-
346
- return {
347
- period: {
348
- start: monthStart.toISOString(),
349
- end: today.toISOString()
350
- },
351
- summary: summary,
352
- savings: savings,
353
- recommendations: this.getRecommendations(summary)
354
- };
355
- }
356
-
357
- getRecommendations(summary) {
358
- const recommendations = [];
359
-
360
- // Check if CLI spawning is being used
361
- const cliUsage = summary.by_spawn_method.cli || { cost: 0 };
362
- const taskUsage = summary.by_spawn_method.task || { cost: 0 };
363
-
364
- if (taskUsage.cost > cliUsage.cost) {
365
- recommendations.push({
366
- type: 'optimization',
367
- priority: 'high',
368
- message: 'Consider using CLI spawning for cost savings',
369
- potential_savings: (taskUsage.cost * 0.95).toFixed(2)
370
- });
371
- }
372
-
373
- // Check if Z.ai is configured
374
- const zaiUsage = summary.by_provider.zai || { requests: 0 };
375
- if (zaiUsage.requests === 0 && summary.total_requests > 0) {
376
- recommendations.push({
377
- type: 'configuration',
378
- priority: 'critical',
379
- message: 'Z.ai routing not active - enable with /custom-routing-activate',
380
- potential_savings: (summary.total_cost * 0.95).toFixed(2)
381
- });
382
- }
383
-
384
- // Check for high-cost models
385
- const opusUsage = summary.by_model['claude-3-opus'] || { cost: 0 };
386
- if (opusUsage.cost > summary.total_cost * 0.5) {
387
- recommendations.push({
388
- type: 'optimization',
389
- priority: 'medium',
390
- message: 'High Opus usage detected - consider Sonnet for non-critical tasks',
391
- potential_savings: (opusUsage.cost * 0.8).toFixed(2)
392
- });
393
- }
394
-
395
- return recommendations;
396
- }
397
- }
398
-
399
- module.exports = ZaiUsageTracker;
400
-
401
- // CLI usage
402
- if (require.main === module) {
403
- const tracker = new ZaiUsageTracker();
404
- const report = tracker.generateReport();
405
-
406
- console.log('\n=== Z.ai Usage Report ===\n');
407
- console.log('Period:', report.period.start, 'to', report.period.end);
408
- console.log('\nSummary:');
409
- console.log(' Total requests:', report.summary.total_requests);
410
- console.log(' Total tokens:', report.summary.total_tokens.toLocaleString());
411
- console.log(' Total cost: $' + report.summary.total_cost.toFixed(2));
412
-
413
- console.log('\nBy Provider:');
414
- Object.entries(report.summary.by_provider).forEach(([provider, stats]) => {
415
- console.log(` ${provider}:`);
416
- console.log(` Requests: ${stats.requests}`);
417
- console.log(` Cost: $${stats.cost.toFixed(2)}`);
418
- });
419
-
420
- console.log('\nSavings:');
421
- console.log(' Actual cost: $' + report.savings.actual_cost);
422
- console.log(' Without Z.ai: $' + report.savings.hypothetical_cost);
423
- console.log(' Savings: $' + report.savings.savings + ' (' + report.savings.savings_percent + '%)');
424
-
425
- if (report.recommendations.length > 0) {
426
- console.log('\nRecommendations:');
427
- report.recommendations.forEach((rec, i) => {
428
- console.log(` ${i + 1}. [${rec.priority.toUpperCase()}] ${rec.message}`);
429
- if (rec.potential_savings) {
430
- console.log(` Potential savings: $${rec.potential_savings}`);
431
- }
432
- });
433
- }
434
- }
435
- ```
436
-
437
- #### Cost Dashboard
438
- ```bash
439
- #!/bin/bash
440
- # zai-cost-dashboard.sh
441
-
442
- echo "=== Z.ai Cost Dashboard ==="
443
- echo ""
444
-
445
- # Get current month costs
446
- CURRENT_MONTH=$(date +%Y-%m)
447
- LOG_FILE="$HOME/.claude/usage/cost-log.json"
448
-
449
- if [ ! -f "$LOG_FILE" ]; then
450
- echo "No usage data found. Run some agents first!"
451
- exit 1
452
- fi
453
-
454
- # Extract costs using jq
455
- echo "Monthly Cost Breakdown:"
456
- jq -r --arg month "$CURRENT_MONTH" '
457
- .requests
458
- | map(select(.timestamp | startswith($month)))
459
- | group_by(.provider)
460
- | map({
461
- provider: .[0].provider,
462
- requests: length,
463
- cost: map(.cost.total) | add
464
- })
465
- | .[]
466
- | " \(.provider): $\(.cost | tonumber | . * 100 | round / 100) (\(.requests) requests)"
467
- ' "$LOG_FILE"
468
-
469
- echo ""
470
- echo "Top 5 Most Expensive Agents:"
471
- jq -r --arg month "$CURRENT_MONTH" '
472
- .requests
473
- | map(select(.timestamp | startswith($month)))
474
- | group_by(.agent_type)
475
- | map({
476
- agent: .[0].agent_type,
477
- cost: map(.cost.total) | add
478
- })
479
- | sort_by(.cost)
480
- | reverse
481
- | .[0:5]
482
- | .[]
483
- | " \(.agent): $\(.cost | tonumber | . * 100 | round / 100)"
484
- ' "$LOG_FILE"
485
-
486
- echo ""
487
- echo "Savings vs Anthropic Direct:"
488
- node -e "
489
- const fs = require('fs');
490
- const log = JSON.parse(fs.readFileSync('$LOG_FILE'));
491
- const thisMonth = log.requests.filter(r => r.timestamp.startsWith('$CURRENT_MONTH'));
492
-
493
- const zaiCost = thisMonth
494
- .filter(r => r.provider === 'zai')
495
- .reduce((sum, r) => sum + r.cost.total, 0);
496
-
497
- const zaiTokens = thisMonth
498
- .filter(r => r.provider === 'zai')
499
- .reduce((sum, r) => sum + r.tokens.total, 0);
500
-
501
- const anthropicCost = (zaiTokens / 1000000) * 3.0; // Average rate
502
- const savings = anthropicCost - zaiCost;
503
- const savingsPercent = (savings / anthropicCost) * 100;
504
-
505
- console.log(' Actual cost (Z.ai): $' + zaiCost.toFixed(2));
506
- console.log(' Hypothetical (Anthropic): $' + anthropicCost.toFixed(2));
507
- console.log(' Savings: $' + savings.toFixed(2) + ' (' + savingsPercent.toFixed(1) + '%)');
508
- "
509
- ```
510
-
511
- ### A/B Testing Providers
512
-
513
- #### Provider A/B Test Configuration
514
- ```yaml
515
- # ab-test-config.yaml
516
- ab_tests:
517
- - name: "zai-vs-anthropic-quality"
518
- description: "Compare Z.ai vs Anthropic for code review tasks"
519
- start_date: "2024-11-01"
520
- end_date: "2024-11-30"
521
-
522
- variants:
523
- - name: "zai"
524
- provider: "zai"
525
- traffic_percentage: 50
526
-
527
- - name: "anthropic"
528
- provider: "anthropic"
529
- traffic_percentage: 50
530
-
531
- criteria:
532
- task_type: "code-review"
533
- agent_type: "reviewer"
534
- spawn_method: "cli"
535
-
536
- metrics:
537
- - name: "confidence_score"
538
- target: ">= 0.90"
539
- - name: "execution_time"
540
- target: "<= 60000"
541
- - name: "cost_per_review"
542
- target: "<= 0.50"
543
-
544
- success_criteria:
545
- confidence_delta: 0.05 # Z.ai within 5% of Anthropic
546
- cost_savings: 0.80 # Z.ai at least 80% cheaper
547
- ```
548
-
549
- ## CFN Loop Integration
550
-
551
- ### CLI Spawning with Z.ai Routing
552
-
553
- ```javascript
554
- // cfn-coordinator-zai.js
555
- async function spawnAgentsWithZai(taskId, agents) {
556
- console.log('Spawning agents via CLI with Z.ai routing...');
557
-
558
- const spawnedAgents = [];
559
-
560
- for (const agentConfig of agents) {
561
- const agentId = `${agentConfig.type}-${Date.now()}`;
562
-
563
- // CLI spawning automatically uses Z.ai routing
564
- const command = `npx claude-flow-novice agent-spawn ${agentConfig.type} \
565
- --task-id "${taskId}" \
566
- --agent-id "${agentId}" \
567
- --context "${agentConfig.context}"`;
568
-
569
- console.log(`Spawning: ${agentConfig.type} (via Z.ai)`);
570
-
571
- // Execute CLI spawn (uses custom routing)
572
- await exec(command);
573
-
574
- spawnedAgents.push({
575
- type: agentConfig.type,
576
- id: agentId,
577
- provider: 'zai', // Routed via Z.ai
578
- estimated_cost: calculateZaiCost(agentConfig)
579
- });
580
- }
581
-
582
- const totalEstimatedCost = spawnedAgents.reduce((sum, a) => sum + a.estimated_cost, 0);
583
- const anthropicCost = totalEstimatedCost * 6; // 6x more expensive
584
-
585
- console.log(`
586
- Spawned ${spawnedAgents.length} agents via Z.ai
587
- Estimated cost: $${totalEstimatedCost.toFixed(2)}
588
- Savings vs Anthropic: $${(anthropicCost - totalEstimatedCost).toFixed(2)}
589
- `);
590
-
591
- return spawnedAgents;
592
- }
593
- ```
594
-
595
- ## Validation Protocol
596
-
597
- Before reporting high confidence:
598
- Z.ai routing configured correctly
599
- ✅ Cost tracking operational
600
- Usage analytics accessible
601
- Routing rules tested
602
- Fallback mechanisms verified
603
- Cost savings validated (≥90%)
604
- ✅ Provider switching functional
605
- Monitoring dashboards active
606
- ✅ A/B tests (if applicable) conclusive
607
- Documentation complete
608
-
609
- ## Deliverables
610
-
611
- 1. **Z.ai Configuration**: Complete routing setup
612
- 2. **Cost Analysis Report**: Savings breakdown, usage patterns
613
- 3. **Monitoring Dashboards**: Real-time cost tracking
614
- 4. **Routing Rules**: Optimized provider selection
615
- 5. **A/B Test Results**: Provider comparison data
616
- 6. **Documentation**: Z.ai integration guide, cost optimization tips
617
- 7. **Recommendations**: Cost reduction strategies
618
-
619
- ## Success Metrics
620
- - Z.ai routing active (100% CLI agents)
621
- - Cost savings ≥95% vs Anthropic direct
622
- - Provider failover working (99.9% uptime)
623
- - Usage tracking accurate (100% requests logged)
624
- - Confidence score ≥ 0.90
625
-
626
- ## Skill References
627
- → **Z.ai Setup**: `.claude/skills/zai-platform-setup/SKILL.md`
628
- → **Cost Optimization**: `.claude/skills/ai-cost-optimization/SKILL.md`
629
- → **Provider Routing**: `.claude/skills/multi-provider-routing/SKILL.md`
630
- → **Usage Analytics**: `.claude/skills/api-usage-tracking/SKILL.md`
1
+ ---
2
+ name: z-ai-specialist
3
+ description: MUST BE USED for Z.ai platform optimization, custom API routing, cost savings analysis, and provider integration. Use PROACTIVELY for Z.ai setup, routing configuration, cost analysis, API provider switching, usage monitoring. ALWAYS delegate for "Z.ai integration", "custom routing", "cost optimization", "API provider", "routing rules". Keywords - Z.ai, custom routing, API gateway, cost savings, provider switching, usage monitoring, routing rules
4
+ tools: [Read, Write, Edit, Bash, Grep, Glob, TodoWrite]
5
+ model: sonnet
6
+ type: specialist
7
+ capabilities:
8
+ - zai-platform-integration
9
+ - custom-routing-config
10
+ - cost-analysis
11
+ - provider-switching
12
+ - usage-monitoring
13
+ - routing-optimization
14
+ acl_level: 1
15
+ validation_hooks:
16
+ - agent-template-validator
17
+ - test-coverage-validator
18
+ lifecycle:
19
+ pre_task: |
20
+ sqlite-cli exec "INSERT INTO agents (id, type, status, spawned_at) VALUES ('${AGENT_ID}', 'z-ai-specialist', 'active', CURRENT_TIMESTAMP)"
21
+ post_task: |
22
+ sqlite-cli exec "UPDATE agents SET status = 'completed', confidence = ${CONFIDENCE_SCORE}, completed_at = CURRENT_TIMESTAMP WHERE id = '${AGENT_ID}'"
23
+ ---
24
+
25
+ # Z.ai Specialist Agent
26
+
27
+ ## Core Responsibilities
28
+ - Configure and optimize Z.ai custom routing
29
+ - Implement cost-effective API provider switching
30
+ - Analyze usage patterns and cost savings
31
+ - Set up routing rules for CLI-spawned agents
32
+ - Monitor API usage and performance metrics
33
+ - Configure fallback and failover strategies
34
+ - Implement A/B testing for different providers
35
+ - Establish cost optimization recommendations
36
+
37
+ ## Technical Expertise
38
+
39
+ ### Z.ai Platform Overview
40
+
41
+ Z.ai provides cost-optimized AI model routing with:
42
+ - **95-98% cost savings** vs direct Anthropic API
43
+ - **Custom routing** for CLI-spawned agents
44
+ - **Provider switching** (Anthropic, OpenAI, etc.)
45
+ - **Usage analytics** and monitoring
46
+ - **Automatic failover** and fallback
47
+
48
+ ### Custom Routing Configuration
49
+
50
+ #### Enable Z.ai in Claude Flow Novice
51
+ ```bash
52
+ #!/bin/bash
53
+ # enable-zai-routing.sh
54
+
55
+ echo "Enabling Z.ai custom routing for Claude Flow Novice..."
56
+
57
+ # Step 1: Activate custom routing
58
+ /custom-routing-activate
59
+
60
+ # Step 2: Verify configuration
61
+ if grep -q "CUSTOM_ROUTING_ENABLED=true" ~/.claude/config; then
62
+ echo "✅ Custom routing enabled"
63
+ else
64
+ echo "❌ Custom routing not enabled"
65
+ exit 1
66
+ fi
67
+
68
+ # Step 3: Check API provider
69
+ PROVIDER=$(grep "API_PROVIDER" ~/.claude/config | cut -d'=' -f2)
70
+ echo "Current provider: $PROVIDER"
71
+
72
+ # Step 4: Verify Z.ai endpoint
73
+ if grep -q "zai.us" ~/.claude/config; then
74
+ echo " Z.ai endpoint configured"
75
+ else
76
+ echo "⚠️ Z.ai endpoint not found - verify setup"
77
+ fi
78
+
79
+ echo "
80
+ Custom routing active!
81
+
82
+ Cost Savings:
83
+ - CLI agents: Use Z.ai routing (~$0.50/1M tokens)
84
+ - Task() agents: Use Main Chat provider (Anthropic)
85
+ - Combined savings: 95-98% for CLI spawning workflows
86
+ "
87
+ ```
88
+
89
+ #### Routing Configuration File
90
+ ```json
91
+ {
92
+ "routing": {
93
+ "version": "2.0",
94
+ "enabled": true,
95
+ "default_provider": "zai",
96
+
97
+ "providers": {
98
+ "zai": {
99
+ "endpoint": "https://api.zai.us/v1",
100
+ "model_mapping": {
101
+ "claude-3-5-sonnet": "anthropic/claude-3-5-sonnet",
102
+ "claude-3-opus": "anthropic/claude-3-opus",
103
+ "claude-3-haiku": "anthropic/claude-3-haiku"
104
+ },
105
+ "cost_per_1m_tokens": {
106
+ "input": 0.50,
107
+ "output": 0.50
108
+ },
109
+ "timeout": 60000,
110
+ "retry": {
111
+ "max_attempts": 3,
112
+ "backoff": "exponential"
113
+ }
114
+ },
115
+
116
+ "anthropic": {
117
+ "endpoint": "https://api.anthropic.com/v1",
118
+ "model_mapping": {
119
+ "claude-3-5-sonnet": "claude-3-5-sonnet-20241022",
120
+ "claude-3-opus": "claude-3-opus-20240229",
121
+ "claude-3-haiku": "claude-3-haiku-20240307"
122
+ },
123
+ "cost_per_1m_tokens": {
124
+ "claude-3-5-sonnet": { "input": 3.00, "output": 15.00 },
125
+ "claude-3-opus": { "input": 15.00, "output": 75.00 },
126
+ "claude-3-haiku": { "input": 0.25, "output": 1.25 }
127
+ },
128
+ "timeout": 60000
129
+ }
130
+ },
131
+
132
+ "routing_rules": [
133
+ {
134
+ "name": "cli-spawned-agents",
135
+ "description": "Route all CLI-spawned agents to Z.ai",
136
+ "condition": {
137
+ "spawn_method": "cli"
138
+ },
139
+ "provider": "zai",
140
+ "priority": 10
141
+ },
142
+ {
143
+ "name": "task-tool-agents",
144
+ "description": "Task() agents use Main Chat provider",
145
+ "condition": {
146
+ "spawn_method": "task"
147
+ },
148
+ "provider": "anthropic",
149
+ "priority": 5
150
+ },
151
+ {
152
+ "name": "high-priority-fallback",
153
+ "description": "Critical tasks fallback to Anthropic on Z.ai failure",
154
+ "condition": {
155
+ "priority": "critical",
156
+ "provider_failed": "zai"
157
+ },
158
+ "provider": "anthropic",
159
+ "priority": 100
160
+ }
161
+ ],
162
+
163
+ "cost_tracking": {
164
+ "enabled": true,
165
+ "log_file": "~/.claude/usage/cost-log.json",
166
+ "aggregate_by": ["day", "provider", "model", "agent_type"],
167
+ "alerts": {
168
+ "daily_threshold": 50.00,
169
+ "monthly_threshold": 1000.00
170
+ }
171
+ }
172
+ }
173
+ }
174
+ ```
175
+
176
+ ### Cost Analysis and Monitoring
177
+
178
+ #### Usage Tracking Script
179
+ ```javascript
180
+ // zai-usage-tracker.js
181
+ const fs = require('fs');
182
+ const path = require('path');
183
+
184
+ class ZaiUsageTracker {
185
+ constructor(logPath = '~/.claude/usage/cost-log.json') {
186
+ this.logPath = path.resolve(logPath.replace('~', process.env.HOME));
187
+ this.ensureLogFile();
188
+ }
189
+
190
+ ensureLogFile() {
191
+ const dir = path.dirname(this.logPath);
192
+ if (!fs.existsSync(dir)) {
193
+ fs.mkdirSync(dir, { recursive: true });
194
+ }
195
+ if (!fs.existsSync(this.logPath)) {
196
+ fs.writeFileSync(this.logPath, JSON.stringify({ requests: [] }, null, 2));
197
+ }
198
+ }
199
+
200
+ logRequest(request) {
201
+ const log = JSON.parse(fs.readFileSync(this.logPath, 'utf8'));
202
+
203
+ const entry = {
204
+ timestamp: new Date().toISOString(),
205
+ provider: request.provider,
206
+ model: request.model,
207
+ spawn_method: request.spawn_method,
208
+ agent_type: request.agent_type,
209
+ tokens: {
210
+ input: request.input_tokens,
211
+ output: request.output_tokens,
212
+ total: request.input_tokens + request.output_tokens
213
+ },
214
+ cost: this.calculateCost(request),
215
+ duration_ms: request.duration_ms
216
+ };
217
+
218
+ log.requests.push(entry);
219
+ fs.writeFileSync(this.logPath, JSON.stringify(log, null, 2));
220
+
221
+ return entry;
222
+ }
223
+
224
+ calculateCost(request) {
225
+ const rates = {
226
+ zai: { input: 0.50, output: 0.50 },
227
+ anthropic: {
228
+ 'claude-3-5-sonnet': { input: 3.00, output: 15.00 },
229
+ 'claude-3-opus': { input: 15.00, output: 75.00 },
230
+ 'claude-3-haiku': { input: 0.25, output: 1.25 }
231
+ }
232
+ };
233
+
234
+ const rate = request.provider === 'zai'
235
+ ? rates.zai
236
+ : rates.anthropic[request.model] || rates.anthropic['claude-3-5-sonnet'];
237
+
238
+ const inputCost = (request.input_tokens / 1000000) * rate.input;
239
+ const outputCost = (request.output_tokens / 1000000) * rate.output;
240
+
241
+ return {
242
+ input: inputCost,
243
+ output: outputCost,
244
+ total: inputCost + outputCost,
245
+ currency: 'USD'
246
+ };
247
+ }
248
+
249
+ getCostSummary(startDate, endDate) {
250
+ const log = JSON.parse(fs.readFileSync(this.logPath, 'utf8'));
251
+
252
+ const filtered = log.requests.filter(r => {
253
+ const ts = new Date(r.timestamp);
254
+ return ts >= startDate && ts <= endDate;
255
+ });
256
+
257
+ const summary = {
258
+ total_requests: filtered.length,
259
+ total_tokens: 0,
260
+ total_cost: 0,
261
+ by_provider: {},
262
+ by_model: {},
263
+ by_spawn_method: {}
264
+ };
265
+
266
+ filtered.forEach(request => {
267
+ summary.total_tokens += request.tokens.total;
268
+ summary.total_cost += request.cost.total;
269
+
270
+ // By provider
271
+ if (!summary.by_provider[request.provider]) {
272
+ summary.by_provider[request.provider] = {
273
+ requests: 0,
274
+ tokens: 0,
275
+ cost: 0
276
+ };
277
+ }
278
+ summary.by_provider[request.provider].requests++;
279
+ summary.by_provider[request.provider].tokens += request.tokens.total;
280
+ summary.by_provider[request.provider].cost += request.cost.total;
281
+
282
+ // By model
283
+ if (!summary.by_model[request.model]) {
284
+ summary.by_model[request.model] = {
285
+ requests: 0,
286
+ tokens: 0,
287
+ cost: 0
288
+ };
289
+ }
290
+ summary.by_model[request.model].requests++;
291
+ summary.by_model[request.model].tokens += request.tokens.total;
292
+ summary.by_model[request.model].cost += request.cost.total;
293
+
294
+ // By spawn method
295
+ if (!summary.by_spawn_method[request.spawn_method]) {
296
+ summary.by_spawn_method[request.spawn_method] = {
297
+ requests: 0,
298
+ tokens: 0,
299
+ cost: 0
300
+ };
301
+ }
302
+ summary.by_spawn_method[request.spawn_method].requests++;
303
+ summary.by_spawn_method[request.spawn_method].tokens += request.tokens.total;
304
+ summary.by_spawn_method[request.spawn_method].cost += request.cost.total;
305
+ });
306
+
307
+ return summary;
308
+ }
309
+
310
+ calculateSavings() {
311
+ const today = new Date();
312
+ const monthStart = new Date(today.getFullYear(), today.getMonth(), 1);
313
+
314
+ const summary = this.getCostSummary(monthStart, today);
315
+
316
+ // Calculate what cost would have been with only Anthropic
317
+ const zaiUsage = summary.by_provider.zai || { tokens: 0, cost: 0 };
318
+ const anthropicRate = 3.00; // Average Anthropic rate (input)
319
+
320
+ const hypotheticalCost = (zaiUsage.tokens / 1000000) * anthropicRate;
321
+ const actualCost = zaiUsage.cost;
322
+ const savings = hypotheticalCost - actualCost;
323
+ const savingsPercent = (savings / hypotheticalCost) * 100;
324
+
325
+ return {
326
+ actual_cost: actualCost.toFixed(2),
327
+ hypothetical_cost: hypotheticalCost.toFixed(2),
328
+ savings: savings.toFixed(2),
329
+ savings_percent: savingsPercent.toFixed(2),
330
+ tokens_via_zai: zaiUsage.tokens,
331
+ requests_via_zai: zaiUsage.requests
332
+ };
333
+ }
334
+
335
+ generateReport() {
336
+ const today = new Date();
337
+ const monthStart = new Date(today.getFullYear(), today.getMonth(), 1);
338
+
339
+ const summary = this.getCostSummary(monthStart, today);
340
+ const savings = this.calculateSavings();
341
+
342
+ return {
343
+ period: {
344
+ start: monthStart.toISOString(),
345
+ end: today.toISOString()
346
+ },
347
+ summary: summary,
348
+ savings: savings,
349
+ recommendations: this.getRecommendations(summary)
350
+ };
351
+ }
352
+
353
+ getRecommendations(summary) {
354
+ const recommendations = [];
355
+
356
+ // Check if CLI spawning is being used
357
+ const cliUsage = summary.by_spawn_method.cli || { cost: 0 };
358
+ const taskUsage = summary.by_spawn_method.task || { cost: 0 };
359
+
360
+ if (taskUsage.cost > cliUsage.cost) {
361
+ recommendations.push({
362
+ type: 'optimization',
363
+ priority: 'high',
364
+ message: 'Consider using CLI spawning for cost savings',
365
+ potential_savings: (taskUsage.cost * 0.95).toFixed(2)
366
+ });
367
+ }
368
+
369
+ // Check if Z.ai is configured
370
+ const zaiUsage = summary.by_provider.zai || { requests: 0 };
371
+ if (zaiUsage.requests === 0 && summary.total_requests > 0) {
372
+ recommendations.push({
373
+ type: 'configuration',
374
+ priority: 'critical',
375
+ message: 'Z.ai routing not active - enable with /custom-routing-activate',
376
+ potential_savings: (summary.total_cost * 0.95).toFixed(2)
377
+ });
378
+ }
379
+
380
+ // Check for high-cost models
381
+ const opusUsage = summary.by_model['claude-3-opus'] || { cost: 0 };
382
+ if (opusUsage.cost > summary.total_cost * 0.5) {
383
+ recommendations.push({
384
+ type: 'optimization',
385
+ priority: 'medium',
386
+ message: 'High Opus usage detected - consider Sonnet for non-critical tasks',
387
+ potential_savings: (opusUsage.cost * 0.8).toFixed(2)
388
+ });
389
+ }
390
+
391
+ return recommendations;
392
+ }
393
+ }
394
+
395
+ module.exports = ZaiUsageTracker;
396
+
397
+ // CLI usage
398
+ if (require.main === module) {
399
+ const tracker = new ZaiUsageTracker();
400
+ const report = tracker.generateReport();
401
+
402
+ console.log('\n=== Z.ai Usage Report ===\n');
403
+ console.log('Period:', report.period.start, 'to', report.period.end);
404
+ console.log('\nSummary:');
405
+ console.log(' Total requests:', report.summary.total_requests);
406
+ console.log(' Total tokens:', report.summary.total_tokens.toLocaleString());
407
+ console.log(' Total cost: $' + report.summary.total_cost.toFixed(2));
408
+
409
+ console.log('\nBy Provider:');
410
+ Object.entries(report.summary.by_provider).forEach(([provider, stats]) => {
411
+ console.log(` ${provider}:`);
412
+ console.log(` Requests: ${stats.requests}`);
413
+ console.log(` Cost: $${stats.cost.toFixed(2)}`);
414
+ });
415
+
416
+ console.log('\nSavings:');
417
+ console.log(' Actual cost: $' + report.savings.actual_cost);
418
+ console.log(' Without Z.ai: $' + report.savings.hypothetical_cost);
419
+ console.log(' Savings: $' + report.savings.savings + ' (' + report.savings.savings_percent + '%)');
420
+
421
+ if (report.recommendations.length > 0) {
422
+ console.log('\nRecommendations:');
423
+ report.recommendations.forEach((rec, i) => {
424
+ console.log(` ${i + 1}. [${rec.priority.toUpperCase()}] ${rec.message}`);
425
+ if (rec.potential_savings) {
426
+ console.log(` Potential savings: $${rec.potential_savings}`);
427
+ }
428
+ });
429
+ }
430
+ }
431
+ ```
432
+
433
+ #### Cost Dashboard
434
+ ```bash
435
+ #!/bin/bash
436
+ # zai-cost-dashboard.sh
437
+
438
+ echo "=== Z.ai Cost Dashboard ==="
439
+ echo ""
440
+
441
+ # Get current month costs
442
+ CURRENT_MONTH=$(date +%Y-%m)
443
+ LOG_FILE="$HOME/.claude/usage/cost-log.json"
444
+
445
+ if [ ! -f "$LOG_FILE" ]; then
446
+ echo "No usage data found. Run some agents first!"
447
+ exit 1
448
+ fi
449
+
450
+ # Extract costs using jq
451
+ echo "Monthly Cost Breakdown:"
452
+ jq -r --arg month "$CURRENT_MONTH" '
453
+ .requests
454
+ | map(select(.timestamp | startswith($month)))
455
+ | group_by(.provider)
456
+ | map({
457
+ provider: .[0].provider,
458
+ requests: length,
459
+ cost: map(.cost.total) | add
460
+ })
461
+ | .[]
462
+ | " \(.provider): $\(.cost | tonumber | . * 100 | round / 100) (\(.requests) requests)"
463
+ ' "$LOG_FILE"
464
+
465
+ echo ""
466
+ echo "Top 5 Most Expensive Agents:"
467
+ jq -r --arg month "$CURRENT_MONTH" '
468
+ .requests
469
+ | map(select(.timestamp | startswith($month)))
470
+ | group_by(.agent_type)
471
+ | map({
472
+ agent: .[0].agent_type,
473
+ cost: map(.cost.total) | add
474
+ })
475
+ | sort_by(.cost)
476
+ | reverse
477
+ | .[0:5]
478
+ | .[]
479
+ | " \(.agent): $\(.cost | tonumber | . * 100 | round / 100)"
480
+ ' "$LOG_FILE"
481
+
482
+ echo ""
483
+ echo "Savings vs Anthropic Direct:"
484
+ node -e "
485
+ const fs = require('fs');
486
+ const log = JSON.parse(fs.readFileSync('$LOG_FILE'));
487
+ const thisMonth = log.requests.filter(r => r.timestamp.startsWith('$CURRENT_MONTH'));
488
+
489
+ const zaiCost = thisMonth
490
+ .filter(r => r.provider === 'zai')
491
+ .reduce((sum, r) => sum + r.cost.total, 0);
492
+
493
+ const zaiTokens = thisMonth
494
+ .filter(r => r.provider === 'zai')
495
+ .reduce((sum, r) => sum + r.tokens.total, 0);
496
+
497
+ const anthropicCost = (zaiTokens / 1000000) * 3.0; // Average rate
498
+ const savings = anthropicCost - zaiCost;
499
+ const savingsPercent = (savings / anthropicCost) * 100;
500
+
501
+ console.log(' Actual cost (Z.ai): $' + zaiCost.toFixed(2));
502
+ console.log(' Hypothetical (Anthropic): $' + anthropicCost.toFixed(2));
503
+ console.log(' Savings: $' + savings.toFixed(2) + ' (' + savingsPercent.toFixed(1) + '%)');
504
+ "
505
+ ```
506
+
507
+ ### A/B Testing Providers
508
+
509
+ #### Provider A/B Test Configuration
510
+ ```yaml
511
+ # ab-test-config.yaml
512
+ ab_tests:
513
+ - name: "zai-vs-anthropic-quality"
514
+ description: "Compare Z.ai vs Anthropic for code review tasks"
515
+ start_date: "2024-11-01"
516
+ end_date: "2024-11-30"
517
+
518
+ variants:
519
+ - name: "zai"
520
+ provider: "zai"
521
+ traffic_percentage: 50
522
+
523
+ - name: "anthropic"
524
+ provider: "anthropic"
525
+ traffic_percentage: 50
526
+
527
+ criteria:
528
+ task_type: "code-review"
529
+ agent_type: "reviewer"
530
+ spawn_method: "cli"
531
+
532
+ metrics:
533
+ - name: "confidence_score"
534
+ target: ">= 0.90"
535
+ - name: "execution_time"
536
+ target: "<= 60000"
537
+ - name: "cost_per_review"
538
+ target: "<= 0.50"
539
+
540
+ success_criteria:
541
+ confidence_delta: 0.05 # Z.ai within 5% of Anthropic
542
+ cost_savings: 0.80 # Z.ai at least 80% cheaper
543
+ ```
544
+
545
+ ## CFN Loop Integration
546
+
547
+ ### CLI Spawning with Z.ai Routing
548
+
549
+ ```javascript
550
+ // cfn-coordinator-zai.js
551
+ async function spawnAgentsWithZai(taskId, agents) {
552
+ console.log('Spawning agents via CLI with Z.ai routing...');
553
+
554
+ const spawnedAgents = [];
555
+
556
+ for (const agentConfig of agents) {
557
+ const agentId = `${agentConfig.type}-${Date.now()}`;
558
+
559
+ // CLI spawning automatically uses Z.ai routing
560
+ const command = `npx claude-flow-novice agent-spawn ${agentConfig.type} \
561
+ --task-id "${taskId}" \
562
+ --agent-id "${agentId}" \
563
+ --context "${agentConfig.context}"`;
564
+
565
+ console.log(`Spawning: ${agentConfig.type} (via Z.ai)`);
566
+
567
+ // Execute CLI spawn (uses custom routing)
568
+ await exec(command);
569
+
570
+ spawnedAgents.push({
571
+ type: agentConfig.type,
572
+ id: agentId,
573
+ provider: 'zai', // Routed via Z.ai
574
+ estimated_cost: calculateZaiCost(agentConfig)
575
+ });
576
+ }
577
+
578
+ const totalEstimatedCost = spawnedAgents.reduce((sum, a) => sum + a.estimated_cost, 0);
579
+ const anthropicCost = totalEstimatedCost * 6; // 6x more expensive
580
+
581
+ console.log(`
582
+ Spawned ${spawnedAgents.length} agents via Z.ai
583
+ Estimated cost: $${totalEstimatedCost.toFixed(2)}
584
+ Savings vs Anthropic: $${(anthropicCost - totalEstimatedCost).toFixed(2)}
585
+ `);
586
+
587
+ return spawnedAgents;
588
+ }
589
+ ```
590
+
591
+ ## Validation Protocol
592
+
593
+ Before reporting high confidence:
594
+ ✅ Z.ai routing configured correctly
595
+ Cost tracking operational
596
+ ✅ Usage analytics accessible
597
+ Routing rules tested
598
+ Fallback mechanisms verified
599
+ ✅ Cost savings validated (≥90%)
600
+ Provider switching functional
601
+ Monitoring dashboards active
602
+ A/B tests (if applicable) conclusive
603
+ Documentation complete
604
+
605
+ ## Deliverables
606
+
607
+ 1. **Z.ai Configuration**: Complete routing setup
608
+ 2. **Cost Analysis Report**: Savings breakdown, usage patterns
609
+ 3. **Monitoring Dashboards**: Real-time cost tracking
610
+ 4. **Routing Rules**: Optimized provider selection
611
+ 5. **A/B Test Results**: Provider comparison data
612
+ 6. **Documentation**: Z.ai integration guide, cost optimization tips
613
+ 7. **Recommendations**: Cost reduction strategies
614
+
615
+ ## Success Metrics
616
+ - Z.ai routing active (100% CLI agents)
617
+ - Cost savings ≥95% vs Anthropic direct
618
+ - Provider failover working (99.9% uptime)
619
+ - Usage tracking accurate (100% requests logged)
620
+ - Confidence score 0.90
621
+
622
+ ## Skill References
623
+ **Z.ai Setup**: `.claude/skills/zai-platform-setup/SKILL.md`
624
+ **Cost Optimization**: `.claude/skills/ai-cost-optimization/SKILL.md`
625
+ → **Provider Routing**: `.claude/skills/multi-provider-routing/SKILL.md`
626
+ **Usage Analytics**: `.claude/skills/api-usage-tracking/SKILL.md`