kalibr 1.1.3a0__py3-none-any.whl → 1.2.1__py3-none-any.whl

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.
@@ -60,6 +60,72 @@ def _get_provider_from_model(model: str) -> str:
60
60
  return "openai"
61
61
 
62
62
 
63
+ def _extract_model_from_agent(agent) -> tuple[str, str]:
64
+ """Extract model name and provider from agent's LLM config.
65
+
66
+ Args:
67
+ agent: CrewAI agent instance
68
+
69
+ Returns:
70
+ Tuple of (model_name, provider)
71
+ """
72
+ model_name = "unknown"
73
+ provider = "openai"
74
+
75
+ if not hasattr(agent, "llm"):
76
+ return model_name, provider
77
+
78
+ llm = agent.llm
79
+
80
+ # Case 1: LLM is a string like "openai/gpt-4o-mini" or "gpt-4"
81
+ if isinstance(llm, str):
82
+ if "/" in llm:
83
+ parts = llm.split("/", 1)
84
+ provider = parts[0]
85
+ model_name = parts[1]
86
+ else:
87
+ model_name = llm
88
+ provider = _get_provider_from_model(llm)
89
+ return model_name, provider
90
+
91
+ # Case 2: LLM has model or model_name attribute
92
+ if hasattr(llm, "model"):
93
+ model_name = str(llm.model)
94
+ elif hasattr(llm, "model_name"):
95
+ model_name = str(llm.model_name)
96
+
97
+ # Parse provider from model string if it contains "/"
98
+ if "/" in model_name:
99
+ parts = model_name.split("/", 1)
100
+ provider = parts[0]
101
+ model_name = parts[1]
102
+ else:
103
+ provider = _get_provider_from_model(model_name)
104
+
105
+ return model_name, provider
106
+
107
+
108
+ def _calculate_cost(provider: str, model: str, input_tokens: int, output_tokens: int) -> float:
109
+ """Calculate cost using CostAdapterFactory.
110
+
111
+ Args:
112
+ provider: Provider name (openai, anthropic, etc.)
113
+ model: Model name
114
+ input_tokens: Number of input tokens
115
+ output_tokens: Number of output tokens
116
+
117
+ Returns:
118
+ Cost in USD
119
+ """
120
+ if CostAdapterFactory is None:
121
+ return 0.0
122
+
123
+ try:
124
+ return CostAdapterFactory.compute_cost(provider, model, input_tokens, output_tokens)
125
+ except Exception:
126
+ return 0.0
127
+
128
+
63
129
  class EventBatcher:
64
130
  """Shared event batching for callbacks."""
65
131
 
@@ -198,6 +264,7 @@ class KalibrAgentCallback:
198
264
  service: Service name
199
265
  workflow_id: Workflow identifier
200
266
  metadata: Additional metadata for all events
267
+ agent: Optional agent reference for model extraction
201
268
 
202
269
  Usage:
203
270
  from kalibr_crewai import KalibrAgentCallback
@@ -210,6 +277,7 @@ class KalibrAgentCallback:
210
277
  goal="Find information",
211
278
  step_callback=callback,
212
279
  )
280
+ callback.set_agent(agent) # Set agent reference for model extraction
213
281
  """
214
282
 
215
283
  def __init__(
@@ -221,6 +289,7 @@ class KalibrAgentCallback:
221
289
  service: Optional[str] = None,
222
290
  workflow_id: Optional[str] = None,
223
291
  metadata: Optional[Dict[str, Any]] = None,
292
+ agent: Optional[Any] = None,
224
293
  ):
225
294
  self.api_key = api_key or os.getenv("KALIBR_API_KEY", "")
226
295
  self.endpoint = endpoint or os.getenv(
@@ -232,6 +301,7 @@ class KalibrAgentCallback:
232
301
  self.service = service or os.getenv("KALIBR_SERVICE", "crewai-app")
233
302
  self.workflow_id = workflow_id or os.getenv("KALIBR_WORKFLOW_ID", "default-workflow")
234
303
  self.default_metadata = metadata or {}
304
+ self._agent = agent
235
305
 
236
306
  # Get shared batcher
237
307
  self._batcher = EventBatcher.get_instance(
@@ -244,6 +314,14 @@ class KalibrAgentCallback:
244
314
  self._agent_span_id: Optional[str] = None
245
315
  self._step_count: int = 0
246
316
 
317
+ def set_agent(self, agent: Any) -> None:
318
+ """Set the agent reference for model extraction.
319
+
320
+ Args:
321
+ agent: CrewAI agent instance
322
+ """
323
+ self._agent = agent
324
+
247
325
  def __call__(self, step_output: Any) -> None:
248
326
  """Called after each agent step.
249
327
 
@@ -271,6 +349,12 @@ class KalibrAgentCallback:
271
349
 
272
350
  span_id = str(uuid.uuid4())
273
351
 
352
+ # Extract model from agent if available
353
+ model_name = "unknown"
354
+ provider = "openai"
355
+ if self._agent:
356
+ model_name, provider = _extract_model_from_agent(self._agent)
357
+
274
358
  # Extract step information
275
359
  step_type = "agent_step"
276
360
  operation = "agent_step"
@@ -307,8 +391,11 @@ class KalibrAgentCallback:
307
391
  output_text = str(step_output)
308
392
 
309
393
  # Count tokens
310
- input_tokens = _count_tokens(tool_input or "", "gpt-4")
311
- output_tokens = _count_tokens(output_text, "gpt-4")
394
+ input_tokens = _count_tokens(tool_input or "", model_name)
395
+ output_tokens = _count_tokens(output_text, model_name)
396
+
397
+ # Calculate cost using CostAdapterFactory
398
+ cost_usd = _calculate_cost(provider, model_name, input_tokens, output_tokens)
312
399
 
313
400
  # Build event
314
401
  event = {
@@ -318,9 +405,9 @@ class KalibrAgentCallback:
318
405
  "parent_span_id": self._agent_span_id,
319
406
  "tenant_id": self.tenant_id,
320
407
  "workflow_id": self.workflow_id,
321
- "provider": "crewai",
322
- "model_id": "agent",
323
- "model_name": "crewai-agent",
408
+ "provider": provider,
409
+ "model_id": model_name,
410
+ "model_name": model_name,
324
411
  "operation": operation,
325
412
  "endpoint": operation,
326
413
  "duration_ms": 0, # Step timing not available
@@ -328,8 +415,8 @@ class KalibrAgentCallback:
328
415
  "input_tokens": input_tokens,
329
416
  "output_tokens": output_tokens,
330
417
  "total_tokens": input_tokens + output_tokens,
331
- "cost_usd": 0.0, # Cost tracked at LLM level
332
- "total_cost_usd": 0.0,
418
+ "cost_usd": cost_usd,
419
+ "total_cost_usd": cost_usd,
333
420
  "status": status,
334
421
  "timestamp": now.isoformat(),
335
422
  "ts_start": now.isoformat(),
@@ -376,6 +463,7 @@ class KalibrTaskCallback:
376
463
  service: Service name
377
464
  workflow_id: Workflow identifier
378
465
  metadata: Additional metadata for all events
466
+ agent: Optional agent reference for model extraction
379
467
 
380
468
  Usage:
381
469
  from kalibr_crewai import KalibrTaskCallback
@@ -388,6 +476,7 @@ class KalibrTaskCallback:
388
476
  agent=my_agent,
389
477
  callback=callback,
390
478
  )
479
+ callback.set_agent(my_agent) # Set agent reference for model extraction
391
480
  """
392
481
 
393
482
  def __init__(
@@ -399,6 +488,7 @@ class KalibrTaskCallback:
399
488
  service: Optional[str] = None,
400
489
  workflow_id: Optional[str] = None,
401
490
  metadata: Optional[Dict[str, Any]] = None,
491
+ agent: Optional[Any] = None,
402
492
  ):
403
493
  self.api_key = api_key or os.getenv("KALIBR_API_KEY", "")
404
494
  self.endpoint = endpoint or os.getenv(
@@ -410,6 +500,7 @@ class KalibrTaskCallback:
410
500
  self.service = service or os.getenv("KALIBR_SERVICE", "crewai-app")
411
501
  self.workflow_id = workflow_id or os.getenv("KALIBR_WORKFLOW_ID", "default-workflow")
412
502
  self.default_metadata = metadata or {}
503
+ self._agent = agent
413
504
 
414
505
  # Get shared batcher
415
506
  self._batcher = EventBatcher.get_instance(
@@ -421,6 +512,14 @@ class KalibrTaskCallback:
421
512
  self._trace_id: Optional[str] = None
422
513
  self._crew_span_id: Optional[str] = None
423
514
 
515
+ def set_agent(self, agent: Any) -> None:
516
+ """Set the agent reference for model extraction.
517
+
518
+ Args:
519
+ agent: CrewAI agent instance
520
+ """
521
+ self._agent = agent
522
+
424
523
  def __call__(self, task_output: Any) -> None:
425
524
  """Called when task completes.
426
525
 
@@ -467,9 +566,18 @@ class KalibrTaskCallback:
467
566
  if hasattr(task_output, "agent"):
468
567
  agent_role = str(task_output.agent)
469
568
 
569
+ # Extract model from agent if available
570
+ model_name = "unknown"
571
+ provider = "openai"
572
+ if self._agent:
573
+ model_name, provider = _extract_model_from_agent(self._agent)
574
+
470
575
  # Token counting
471
- input_tokens = _count_tokens(description, "gpt-4")
472
- output_tokens = _count_tokens(raw_output, "gpt-4")
576
+ input_tokens = _count_tokens(description, model_name)
577
+ output_tokens = _count_tokens(raw_output, model_name)
578
+
579
+ # Calculate cost using CostAdapterFactory
580
+ cost_usd = _calculate_cost(provider, model_name, input_tokens, output_tokens)
473
581
 
474
582
  # Build operation name from description
475
583
  operation = "task_complete"
@@ -486,9 +594,9 @@ class KalibrTaskCallback:
486
594
  "parent_span_id": self._crew_span_id,
487
595
  "tenant_id": self.tenant_id,
488
596
  "workflow_id": self.workflow_id,
489
- "provider": "crewai",
490
- "model_id": "task",
491
- "model_name": agent_role,
597
+ "provider": provider,
598
+ "model_id": model_name,
599
+ "model_name": model_name,
492
600
  "operation": operation,
493
601
  "endpoint": "task_complete",
494
602
  "duration_ms": 0, # Task timing not available in callback
@@ -496,8 +604,8 @@ class KalibrTaskCallback:
496
604
  "input_tokens": input_tokens,
497
605
  "output_tokens": output_tokens,
498
606
  "total_tokens": input_tokens + output_tokens,
499
- "cost_usd": 0.0, # Cost tracked at LLM level
500
- "total_cost_usd": 0.0,
607
+ "cost_usd": cost_usd,
608
+ "total_cost_usd": cost_usd,
501
609
  "status": "success",
502
610
  "timestamp": now.isoformat(),
503
611
  "ts_start": now.isoformat(),
@@ -21,6 +21,72 @@ except ImportError:
21
21
  CostAdapterFactory = None
22
22
 
23
23
 
24
+ def _extract_model_from_agent(agent) -> tuple[str, str]:
25
+ """Extract model name and provider from agent's LLM config.
26
+
27
+ Args:
28
+ agent: CrewAI agent instance
29
+
30
+ Returns:
31
+ Tuple of (model_name, provider)
32
+ """
33
+ model_name = "unknown"
34
+ provider = "openai"
35
+
36
+ if not hasattr(agent, "llm"):
37
+ return model_name, provider
38
+
39
+ llm = agent.llm
40
+
41
+ # Case 1: LLM is a string like "openai/gpt-4o-mini" or "gpt-4"
42
+ if isinstance(llm, str):
43
+ if "/" in llm:
44
+ parts = llm.split("/", 1)
45
+ provider = parts[0]
46
+ model_name = parts[1]
47
+ else:
48
+ model_name = llm
49
+ provider = _get_provider_from_model(llm)
50
+ return model_name, provider
51
+
52
+ # Case 2: LLM has model or model_name attribute
53
+ if hasattr(llm, "model"):
54
+ model_name = str(llm.model)
55
+ elif hasattr(llm, "model_name"):
56
+ model_name = str(llm.model_name)
57
+
58
+ # Parse provider from model string if it contains "/"
59
+ if "/" in model_name:
60
+ parts = model_name.split("/", 1)
61
+ provider = parts[0]
62
+ model_name = parts[1]
63
+ else:
64
+ provider = _get_provider_from_model(model_name)
65
+
66
+ return model_name, provider
67
+
68
+
69
+ def _calculate_cost(provider: str, model: str, input_tokens: int, output_tokens: int) -> float:
70
+ """Calculate cost using CostAdapterFactory.
71
+
72
+ Args:
73
+ provider: Provider name (openai, anthropic, etc.)
74
+ model: Model name
75
+ input_tokens: Number of input tokens
76
+ output_tokens: Number of output tokens
77
+
78
+ Returns:
79
+ Cost in USD
80
+ """
81
+ if CostAdapterFactory is None:
82
+ return 0.0
83
+
84
+ try:
85
+ return CostAdapterFactory.compute_cost(provider, model, input_tokens, output_tokens)
86
+ except Exception:
87
+ return 0.0
88
+
89
+
24
90
  class KalibrCrewAIInstrumentor:
25
91
  """Auto-instrumentation for CrewAI.
26
92
 
@@ -341,6 +407,9 @@ class KalibrCrewAIInstrumentor:
341
407
  role = getattr(agent_self, "role", "unknown")
342
408
  goal = getattr(agent_self, "goal", "")
343
409
 
410
+ # Extract model from agent's LLM config
411
+ model_name, provider = _extract_model_from_agent(agent_self)
412
+
344
413
  # Get task info
345
414
  task_description = ""
346
415
  if hasattr(task, "description"):
@@ -370,8 +439,11 @@ class KalibrCrewAIInstrumentor:
370
439
  output_preview = str(result)[:500]
371
440
 
372
441
  # Token estimation
373
- input_tokens = _count_tokens(task_description + goal, "gpt-4")
374
- output_tokens = _count_tokens(output_preview or "", "gpt-4")
442
+ input_tokens = _count_tokens(task_description + goal, model_name)
443
+ output_tokens = _count_tokens(output_preview or "", model_name)
444
+
445
+ # Calculate cost using CostAdapterFactory
446
+ cost_usd = _calculate_cost(provider, model_name, input_tokens, output_tokens)
375
447
 
376
448
  event = {
377
449
  "schema_version": "1.0",
@@ -380,9 +452,9 @@ class KalibrCrewAIInstrumentor:
380
452
  "parent_span_id": None,
381
453
  "tenant_id": instrumentor.tenant_id,
382
454
  "workflow_id": instrumentor.workflow_id,
383
- "provider": "crewai",
384
- "model_id": "agent",
385
- "model_name": role,
455
+ "provider": provider,
456
+ "model_id": model_name,
457
+ "model_name": model_name,
386
458
  "operation": f"agent:{role}",
387
459
  "endpoint": "agent.execute_task",
388
460
  "duration_ms": duration_ms,
@@ -390,8 +462,8 @@ class KalibrCrewAIInstrumentor:
390
462
  "input_tokens": input_tokens,
391
463
  "output_tokens": output_tokens,
392
464
  "total_tokens": input_tokens + output_tokens,
393
- "cost_usd": 0.0,
394
- "total_cost_usd": 0.0,
465
+ "cost_usd": cost_usd,
466
+ "total_cost_usd": cost_usd,
395
467
  "status": status,
396
468
  "error_type": error_type,
397
469
  "error_message": error_message,
@@ -430,6 +502,13 @@ class KalibrCrewAIInstrumentor:
430
502
  description = getattr(task_self, "description", "")
431
503
  expected_output = getattr(task_self, "expected_output", "")
432
504
 
505
+ # Try to extract model from task's agent
506
+ model_name = "unknown"
507
+ provider = "openai"
508
+ agent = getattr(task_self, "agent", None)
509
+ if agent:
510
+ model_name, provider = _extract_model_from_agent(agent)
511
+
433
512
  status = "success"
434
513
  error_type = None
435
514
  error_message = None
@@ -456,8 +535,11 @@ class KalibrCrewAIInstrumentor:
456
535
  else:
457
536
  output_preview = str(result)[:500]
458
537
 
459
- input_tokens = _count_tokens(description, "gpt-4")
460
- output_tokens = _count_tokens(output_preview or "", "gpt-4")
538
+ input_tokens = _count_tokens(description, model_name)
539
+ output_tokens = _count_tokens(output_preview or "", model_name)
540
+
541
+ # Calculate cost using CostAdapterFactory
542
+ cost_usd = _calculate_cost(provider, model_name, input_tokens, output_tokens)
461
543
 
462
544
  event = {
463
545
  "schema_version": "1.0",
@@ -466,9 +548,9 @@ class KalibrCrewAIInstrumentor:
466
548
  "parent_span_id": None,
467
549
  "tenant_id": instrumentor.tenant_id,
468
550
  "workflow_id": instrumentor.workflow_id,
469
- "provider": "crewai",
470
- "model_id": "task",
471
- "model_name": "crewai-task",
551
+ "provider": provider,
552
+ "model_id": model_name,
553
+ "model_name": model_name,
472
554
  "operation": f"task:{description[:30]}..." if len(description) > 30 else f"task:{description}",
473
555
  "endpoint": "task.execute_sync",
474
556
  "duration_ms": duration_ms,
@@ -476,8 +558,8 @@ class KalibrCrewAIInstrumentor:
476
558
  "input_tokens": input_tokens,
477
559
  "output_tokens": output_tokens,
478
560
  "total_tokens": input_tokens + output_tokens,
479
- "cost_usd": 0.0,
480
- "total_cost_usd": 0.0,
561
+ "cost_usd": cost_usd,
562
+ "total_cost_usd": cost_usd,
481
563
  "status": status,
482
564
  "error_type": error_type,
483
565
  "error_message": error_message,
@@ -29,7 +29,7 @@ Usage:
29
29
 
30
30
  Environment Variables:
31
31
  KALIBR_API_KEY: API key for authentication
32
- KALIBR_ENDPOINT: Backend endpoint URL
32
+ KALIBR_COLLECTOR_URL: Backend endpoint URL
33
33
  KALIBR_TENANT_ID: Tenant identifier
34
34
  KALIBR_ENVIRONMENT: Environment (prod/staging/dev)
35
35
  KALIBR_SERVICE: Service name
@@ -29,6 +29,8 @@ try:
29
29
  except ImportError:
30
30
  CostAdapterFactory = None
31
31
 
32
+ from kalibr.context import get_goal
33
+
32
34
  # Import tiktoken for token counting
33
35
  try:
34
36
  import tiktoken
@@ -288,6 +290,25 @@ class KalibrCallbackHandler(BaseCallbackHandler):
288
290
  # Compute cost
289
291
  cost_usd = self._compute_cost(provider, model, input_tokens, output_tokens)
290
292
 
293
+ # Extract tool_id from operation if this is a tool span
294
+ tool_id = ""
295
+ tool_input = ""
296
+ tool_output = ""
297
+
298
+ if span.get("span_type") == "tool":
299
+ operation = span.get("operation", "")
300
+ if operation.startswith("tool:"):
301
+ tool_id = operation[5:] # Extract "browserless" from "tool:browserless"
302
+
303
+ # Get tool input/output from span (truncate to 10KB)
304
+ if span.get("input"):
305
+ tool_input = str(span["input"])[:10000]
306
+ if metadata and metadata.get("output"):
307
+ tool_output = str(metadata["output"])[:10000]
308
+
309
+ # Get goal from context (thread-safe)
310
+ current_goal = get_goal() or ""
311
+
291
312
  # Build event
292
313
  event = {
293
314
  "schema_version": "1.0",
@@ -318,6 +339,11 @@ class KalibrCallbackHandler(BaseCallbackHandler):
318
339
  "service": self.service,
319
340
  "runtime_env": os.getenv("RUNTIME_ENV", "local"),
320
341
  "sandbox_id": os.getenv("SANDBOX_ID", "local"),
342
+ # New fields for tool/goal tracking
343
+ "tool_id": tool_id,
344
+ "tool_input": tool_input,
345
+ "tool_output": tool_output,
346
+ "goal": current_goal,
321
347
  "metadata": {
322
348
  **self.default_metadata,
323
349
  "span_type": span.get("span_type", "llm"),
@@ -26,7 +26,7 @@ Usage:
26
26
 
27
27
  Environment Variables:
28
28
  KALIBR_API_KEY: API key for authentication
29
- KALIBR_ENDPOINT: Backend endpoint URL
29
+ KALIBR_COLLECTOR_URL: Backend endpoint URL
30
30
  KALIBR_TENANT_ID: Tenant identifier
31
31
  KALIBR_ENVIRONMENT: Environment (prod/staging/dev)
32
32
  KALIBR_SERVICE: Service name