Initial travel knowledge graph release
This commit is contained in:
43
app/agents/call_log.py
Normal file
43
app/agents/call_log.py
Normal file
@@ -0,0 +1,43 @@
|
||||
"""Agent call logging — cost, performance, error tracking."""
|
||||
import time
|
||||
from contextlib import asynccontextmanager
|
||||
from typing import AsyncIterator
|
||||
|
||||
from app.config import settings
|
||||
from app.db import log_agent_call
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def logged_agent_call(
|
||||
agent_name: str,
|
||||
project_id: str | None = None,
|
||||
actor: str | None = None,
|
||||
request_id: str | None = None,
|
||||
model: str | None = None,
|
||||
) -> AsyncIterator[dict]:
|
||||
"""Context manager that logs agent call on exit."""
|
||||
start = time.monotonic()
|
||||
entry = {
|
||||
"agent_name": agent_name,
|
||||
"project_id": project_id or settings.default_project,
|
||||
"actor": actor,
|
||||
"request_id": request_id,
|
||||
"model": model or settings.llm_model,
|
||||
"prompt_chars": 0,
|
||||
"response_chars": 0,
|
||||
"latency_ms": 0,
|
||||
"status": "error",
|
||||
"error_message": None,
|
||||
}
|
||||
try:
|
||||
yield entry
|
||||
entry["status"] = "success"
|
||||
except Exception as exc:
|
||||
entry["status"] = "error"
|
||||
entry["error_message"] = str(exc)[:500]
|
||||
finally:
|
||||
entry["latency_ms"] = int((time.monotonic() - start) * 1000)
|
||||
try:
|
||||
await log_agent_call(entry)
|
||||
except Exception:
|
||||
pass
|
||||
Reference in New Issue
Block a user