pyagent-trace API Reference¶
Event Bus¶
pyagent_trace.events.TraceEvent
dataclass
¶
Contract: every trace event pyagent emits.
Attributes:
| Name | Type | Description |
|---|---|---|
timestamp |
float
|
Unix timestamp of the event. |
event_type |
str
|
One of pattern_start, pattern_end, agent_start, agent_end, llm_call, llm_response, routing_decision, compression, error, cost_record. |
agent_name |
str
|
Name of the agent involved (empty string if N/A). |
pattern_type |
str
|
Name of the pattern involved (empty string if N/A). |
payload |
dict[str, Any]
|
Event-specific data (tokens, cost, duration, model, messages, etc.). |
Source code in packages/pyagent-trace/src/pyagent_trace/events.py
pyagent_trace.events.TraceEventBus
¶
Pub/sub event bus — the contract between trace producers and studio consumers.
Usage
bus = TraceEventBus() sub_id = bus.subscribe(lambda event: print(event)) bus.emit(TraceEvent(timestamp=time.time(), event_type="llm_call", ...)) bus.unsubscribe(sub_id)
Source code in packages/pyagent-trace/src/pyagent_trace/events.py
emit(event)
¶
Emit event to all matching subscribers (sync).
Source code in packages/pyagent-trace/src/pyagent_trace/events.py
emit_async(event)
async
¶
Emit event to all matching subscribers, awaiting any coroutine callbacks.
Source code in packages/pyagent-trace/src/pyagent_trace/events.py
subscribe(callback)
¶
Subscribe to all trace events. Returns subscription ID.
subscribe_filter(event_types, callback)
¶
Subscribe to specific event types only. Returns subscription ID.
Source code in packages/pyagent-trace/src/pyagent_trace/events.py
unsubscribe(subscription_id)
¶
Exporters¶
pyagent_trace.exporters.base.TraceExporter
¶
Bases: Protocol
Portal-agnostic exporter contract.
Any class implementing these three methods can receive pyagent trace events. Built-in: ConsoleExporter, JsonlExporter, OTelExporter, LangfuseExporter. Users can implement custom exporters for any backend.
Source code in packages/pyagent-trace/src/pyagent_trace/exporters/base.py
pyagent_trace.exporters.console.ConsoleExporter
¶
Export trace events to stdout (or any file-like object).
Usage
exporter = ConsoleExporter() bus.subscribe(exporter.export_event)
Source code in packages/pyagent-trace/src/pyagent_trace/exporters/console.py
export_event(event)
¶
Print a formatted trace event.
Source code in packages/pyagent-trace/src/pyagent_trace/exporters/console.py
flush()
¶
pyagent_trace.exporters.jsonl.JsonlExporter
¶
Export trace events to a JSONL file.
Usage
exporter = JsonlExporter("traces/run_001.jsonl") bus.subscribe(exporter.export_event)
Source code in packages/pyagent-trace/src/pyagent_trace/exporters/jsonl.py
pyagent_trace.exporters.langfuse.LangfuseExporter
¶
Export trace events to Langfuse.
Usage
exporter = LangfuseExporter( public_key="pk-...", secret_key="sk-...", host="https://cloud.langfuse.com", ) bus.subscribe(exporter.export_event)
Requires: pip install pyagent-trace[langfuse]
Source code in packages/pyagent-trace/src/pyagent_trace/exporters/langfuse.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | |
Spans¶
pyagent_trace.spans.PatternSpanEmitter
¶
Emit OTel spans for pattern executions.
Usage
emitter = PatternSpanEmitter() with emitter.pattern_span("debate", {"rounds": 3}) as span: # ... pattern logic ... emitter.set_result(span, result)
Source code in packages/pyagent-trace/src/pyagent_trace/spans.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | |
agent_span(agent_name, parent_span=None, attributes=None)
¶
Start a span for an individual agent call.
Source code in packages/pyagent-trace/src/pyagent_trace/spans.py
pattern_span(pattern_type, attributes=None)
¶
Start a span for a pattern execution.
Source code in packages/pyagent-trace/src/pyagent_trace/spans.py
set_compression_info(span, input_tokens, output_tokens, savings_pct)
staticmethod
¶
Set compression attributes on a span.
Source code in packages/pyagent-trace/src/pyagent_trace/spans.py
set_error(span, error)
staticmethod
¶
set_pattern_result(span, output_length, rounds=None, consensus=None, escalated=False, duration_ms=0.0, token_estimate=0, cost_estimate=0.0)
staticmethod
¶
Set result attributes on a pattern span.
Source code in packages/pyagent-trace/src/pyagent_trace/spans.py
set_routing_info(span, difficulty, selected_model, cost_estimate, category='')
staticmethod
¶
Set routing attributes on a span.
Source code in packages/pyagent-trace/src/pyagent_trace/spans.py
pyagent_trace.attributes.PyAgentAttributes
¶
Attribute key constants for pyagent OTel spans.
Source code in packages/pyagent-trace/src/pyagent_trace/attributes.py
Decorators¶
pyagent_trace.decorators.traced_pattern(cls)
¶
Class decorator: auto-emit OTel spans for every pattern.run() call.
Usage
@traced_pattern class MyPattern(Pattern): ...
Source code in packages/pyagent-trace/src/pyagent_trace/decorators.py
pyagent_trace.decorators.traced_agent(agent)
¶
Wrap an Agent instance to emit OTel spans on every run() call.
Usage
agent = traced_agent(Agent("my_agent", llm))
Source code in packages/pyagent-trace/src/pyagent_trace/decorators.py
Cost Tracking¶
pyagent_trace.cost.CostTracker
¶
Track costs across an entire workflow.
Usage
tracker = CostTracker() tracker.record("debate", "bull_agent", "gpt-4o", 500, 200, 0.003) tracker.record("debate", "bear_agent", "gpt-4o-mini", 500, 200, 0.0004) print(tracker.summary())
Source code in packages/pyagent-trace/src/pyagent_trace/cost.py
by_agent()
¶
Cost breakdown by agent name.
by_model()
¶
Cost breakdown by model.
by_pattern()
¶
Cost breakdown by pattern type.
record(pattern_type, agent_name, model, input_tokens, output_tokens, cost_usd)
¶
Record a cost entry.
Source code in packages/pyagent-trace/src/pyagent_trace/cost.py
summary()
¶
Full cost summary.
Source code in packages/pyagent-trace/src/pyagent_trace/cost.py
pyagent_trace.cost.CostEntry
dataclass
¶
Record & Replay¶
pyagent_trace.recorder.Recorder
¶
Record pattern executions for debugging and replay.
Usage
recorder = Recorder() recorder.start("debate") recorder.record_llm_call("bull", messages, response) recorder.save("debug_trace.jsonl")
Source code in packages/pyagent-trace/src/pyagent_trace/recorder.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | |
end(result_output)
¶
Mark the end of a pattern execution.
Source code in packages/pyagent-trace/src/pyagent_trace/recorder.py
load(path)
classmethod
¶
Load recorded entries from a JSONL file.
Source code in packages/pyagent-trace/src/pyagent_trace/recorder.py
record_llm_call(agent_name, messages, response, metadata=None)
¶
Record an LLM call and its response.
Source code in packages/pyagent-trace/src/pyagent_trace/recorder.py
save(path)
¶
Save recorded entries to a JSONL file.
Source code in packages/pyagent-trace/src/pyagent_trace/recorder.py
start(pattern_type)
¶
Mark the start of a pattern execution.
Source code in packages/pyagent-trace/src/pyagent_trace/recorder.py
pyagent_trace.recorder.RecordEntry
dataclass
¶
A single recorded event.