mirror of
https://github.com/x1xhlol/system-prompts-and-models-of-ai-tools.git
synced 2026-06-17 23:09:35 +00:00
AI Layer: - llm_router.py: routes cheap/mid/high models, enforces daily budget, caches - token_counter.py: estimates tokens, truncates to budget - response_cache.py: in-memory cache with TTL per agent - prompt_registry.py: versioned prompts with stable prefix for caching - ai_budget.yaml: model costs, agent budgets, daily limits (10 SAR/day) Guardrails: - output_validator.py: blocks fake claims + prohibited actions - cost_guard.py: prevents runaway spending Observability: - trace.py: trace_id, cost, latency, steps per pipeline run Tests: ALL PASS - 30/30 evals (100%) — 9 sectors, 30 companies - 10/10 prohibited actions blocked - 4/4 allowed actions verified - 3/3 forbidden claims blocked - 3/3 message quality checks passed https://claude.ai/code/session_01W1rJthWDkasijTdXCfxVHs
42 lines
1.4 KiB
Python
42 lines
1.4 KiB
Python
"""Pipeline tracing — tracks cost, latency, decisions per run."""
|
|
import time
|
|
import uuid
|
|
import json
|
|
import logging
|
|
|
|
logger = logging.getLogger("dealix.gtm_os.trace")
|
|
|
|
class PipelineTrace:
|
|
def __init__(self, pipeline_name: str, company: str = ""):
|
|
self.trace_id = str(uuid.uuid4())[:8]
|
|
self.pipeline = pipeline_name
|
|
self.company = company
|
|
self.start_time = time.time()
|
|
self.steps: list[dict] = []
|
|
self.total_cost = 0.0
|
|
|
|
def log_step(self, agent: str, result_summary: str, cost: float = 0.0, latency_ms: float = 0.0):
|
|
step = {
|
|
"agent": agent,
|
|
"result": result_summary[:200],
|
|
"cost_sar": round(cost, 6),
|
|
"latency_ms": round(latency_ms, 1),
|
|
"timestamp": time.time(),
|
|
}
|
|
self.steps.append(step)
|
|
self.total_cost += cost
|
|
|
|
def finish(self) -> dict:
|
|
elapsed = time.time() - self.start_time
|
|
report = {
|
|
"trace_id": self.trace_id,
|
|
"pipeline": self.pipeline,
|
|
"company": self.company,
|
|
"total_time_s": round(elapsed, 2),
|
|
"total_cost_sar": round(self.total_cost, 6),
|
|
"steps": len(self.steps),
|
|
"step_details": self.steps,
|
|
}
|
|
logger.info(f"[TRACE:{self.trace_id}] {self.pipeline} for {self.company}: {elapsed:.1f}s, {self.total_cost:.4f} SAR, {len(self.steps)} steps")
|
|
return report
|