Manual Tracing
Tracing operations
See the Quick Start for instructions on installing and configuring the Graphsignal tracer.
Python
To measure and monitor operations that are not automatically instrumented, e.g. tool calls, wrap the code with trace()
method or use @trace_function
decorator.
To record payloads and track usage metrics, use Span.set_payload()
.
with graphsignal.trace('my-tool') as span:
...
span.set_payload('my-data', data, usage=dict(size=my_data_size))
You can also record sub-operations to visualize operation timeline and latency breakdown.
with graphsignal.trace('inference') as op_span:
with span.trace('model-cold-boot') as subop_span:
...
@graphsignal.trace_function
def my_function():
...
Tracing LLM calls
When tracing LLM generations, provide payloads in OpenAI format, which is supported by Graphsignal. Set model_type='chat'
tag and add request and response data as request
and response
payloads respectively.
Python
with graphsignal.trace('generate-completion', tags=dict(model_type='chat')) as span:
res_data = my_llm_call(req_data)
...
span.set_payload('request', req_data)
span.set_payload('response', res_data)
...
span.set_usage('prompt_tokens', prompt_tokens)
span.set_usage('completion_tokens', completion_tokens)
Exceptions
Python
For auto-instrumented libraries, or when using @trace_function
decorator, trace()
method with with
context manager or callbacks, exceptions are automatically recorded. For other cases, use Span.add_exception
.