python
1import time
2import uuid
3from contextlib import contextmanager
4
5@contextmanager
6def llm_span(name: str, **attrs: object):
7 trace_id = str(uuid.uuid4())
8 started = time.perf_counter()
9 try:
10 yield trace_id
11 finally:
12 elapsed_ms = int((time.perf_counter() - started) * 1000)
13 event = {"trace_id": trace_id, "name": name, "latency_ms": elapsed_ms, **attrs}
14 print(event)
15
16with llm_span("Cost Tracking", model="gpt-4o-mini", prompt_tokens=128):
17 response = "Grounded answer with cited context."
18print(response)