Download backend/agent_observability.py from zkwr1081/Agent-Observability-Lab: direct link, hf CLI and curl.
- Browser
- Download file 1.38 kB
-
https://huggingface.co/spaces/zkwr1081/Agent-Observability-Lab/resolve/main/backend/agent_observability.py
- Command line
-
hf download hf://spaces/zkwr1081/Agent-Observability-Lab/backend/agent_observability.py
-
curl -L -o agent_observability.py https://huggingface.co/spaces/zkwr1081/Agent-Observability-Lab/resolve/main/backend/agent_observability.py
1.38 kB
| """Minimal sender for Agent Observability API. Never send secrets or raw PII.""" | |
| import json, os, urllib.request | |
| class AgentObservability: | |
| def __init__(self, endpoint: str, api_key: str | None = None, timeout: float = 8.0): | |
| self.endpoint=endpoint.rstrip('/') | |
| self.api_key=api_key or os.environ.get('AGENT_OBS_API_KEY','') | |
| self.timeout=timeout | |
| if not self.endpoint.startswith(('http://127.0.0.1','http://localhost','https://')): | |
| raise ValueError('Use localhost for local development or HTTPS for remote endpoints') | |
| if not self.api_key: raise ValueError('AGENT_OBS_API_KEY is required') | |
| def record(self, *, agent_name, status, duration_ms, input_tokens=0, output_tokens=0, estimated_cost_usd=0, error_type=None, request_id=None): | |
| body=json.dumps({ | |
| 'agent_name':agent_name,'status':status,'duration_ms':duration_ms, | |
| 'input_tokens':input_tokens,'output_tokens':output_tokens, | |
| 'estimated_cost_usd':estimated_cost_usd,'error_type':error_type,'request_id':request_id | |
| }).encode() | |
| req=urllib.request.Request(self.endpoint+'/api/v1/traces',data=body,headers={'Content-Type':'application/json','X-API-Key':self.api_key},method='POST') | |
| with urllib.request.urlopen(req,timeout=self.timeout) as response: | |
| return json.loads(response.read().decode()) | |