1
Install TraceLLM and Dependencies
Begin by installing the TraceLLM package via pip. This framework requires a stable Python environment (3.9+). Run the following command in your terminal to install the core library along with its recommended dependencies for visualization and logging:
```bash
pip install tracellm
```
Ensure you have a working LLM backend configured, such as OpenAI, Anthropic, or a local Hugging Face model. TraceLLM acts as a middleware or wrapper, so it needs access to your standard AI SDK. For this tutorial, we will assume you have the `openai` package installed. If you are using a local model, ensure `transformers` and `accelerate` are present, as TraceLLM hooks into the forward pass of these libraries to capture activation data. Verify the installation by importing the library in a Python shell without errors.
Pro Tip
Use a virtual environment to avoid dependency conflicts with existing ML libraries.
2
Initialize the Tracer Context
Before making any LLM calls, you must initialize the TraceLLM context manager. This sets up the hooks necessary to intercept model inputs and outputs. Import the `Tracer` class from the `tracellm` module. You should initialize it with your API key and specify the granularity of the trace. For debugging reasoning, set `mode='detailed'` to capture token-level activations.
```python
from tracellm import Tracer
# Initialize with your API key and detailed tracing mode
tracer = Tracer(api_key="your_api_key", mode="detailed")
```
The tracer will automatically patch the underlying LLM client. If you are using OpenAI, TraceLLM intercepts the `chat.completions.create` method. Ensure this initialization happens before any AI requests are made in your application lifecycle to prevent missing critical early-token data.
Pro Tip
Always initialize the tracer at the start of your script or application entry point to ensure no requests are missed.
3
Execute a Prompt with Tracing
Now, write a simple script to send a prompt to your LLM. Use the context manager provided by TraceLLM to wrap your inference call. This ensures that all internal states during generation are captured. For example, if you are using the OpenAI API, wrap your call within the `tracer.trace()` context.
```python
import openai
with tracer.trace(session_id="debug_session_01"):
response = openai.ChatCompletion.create(
model="gpt-4-turbo",
messages=[{"role": "user", "content": "Explain quantum entanglement simply."}]
)
print(response.choices[0].message.content)
```
The `session_id` parameter is crucial for grouping related traces. When the code executes, TraceLLM will capture the input tokens, the hidden states at each layer, and the final output logits. This data is stored in memory or written to a local buffer, depending on your configuration.
Pro Tip
Use unique session IDs for different test cases to keep your debugging logs organized and searchable.
4
Visualize the Execution Trace
After the inference completes, use TraceLLMโs built-in viewer to analyze the execution. You can launch a local web server or generate a static HTML report. Run the following command to start the dashboard:
```bash
tracellm serve
```
Navigate to `http://localhost:8000` in your browser. You will see a timeline view of the `debug_session_01`. Click on the specific token generation step to expand the activation maps. The visualization highlights which neuron groups fired most strongly for each token, allowing you to see the 'reasoning path.' Look for high-activation clusters that correspond to key concepts in the prompt, such as 'quantum' or 'entanglement.' This visual feedback helps identify if the model is focusing on relevant context or getting distracted by noise.
Pro Tip
Use the 'Compare' feature in the dashboard to run two prompts side-by-side and highlight differences in activation patterns.
5
Identify and Resolve Hallucinations
One of the most powerful uses of TraceLLM is debugging hallucinations. If the model produces an incorrect fact, inspect the trace leading up to that token. In the dashboard, select the erroneous token and view the 'Attention Flow' graph. This shows which input tokens influenced the current prediction. If you see high attention weights on irrelevant parts of the context window, you may need to adjust your prompt engineering or system instructions. For instance, if the model hallucinates a date, check if the attention is leaking from unrelated dates in the context. Use this insight to refine your prompt structure or add negative constraints to improve accuracy.
Pro Tip
Focus on the 'last 5 tokens' of the trace before the error; this is where the model's decision boundary is most visible.
6
Export and Share Trace Data
For collaborative debugging or long-term auditing, export the trace data as a JSON file. This format is compatible with most data analysis tools. Use the CLI command to export all sessions from the current cache:
```bash
tracellm export --format json --output ./traces
```
You can also share these traces with your team via the TraceLLM cloud platform if you have a team account. This allows other developers to view the same execution path without needing to re-run the expensive inference. The JSON file contains the raw logits, attention weights, and token IDs, which can be loaded into Jupyter notebooks for deeper statistical analysis or custom visualization scripts.
Pro Tip
Automate exports in your CI/CD pipeline to maintain a historical record of model behavior changes.