> For the complete documentation index, see [llms.txt](https://klaralabs.gitbook.io/klara-labs-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://klaralabs.gitbook.io/klara-labs-docs/getting-started/advanced-token-level-analysis.md).

# Advanced Token Level Analysis

```python
import requests
import json

# Set your API key
api_key = "YOUR_API_KEY"
url = "https://api.klaralabs.com/detect"
headers = {
    "X-API-Key": api_key,
    "Content-Type": "application/json"
}

# Example data with potential hallucination
data = {
    "context": ["The Earth is the third planet from the Sun."],
    "question": "What is Earth's position in the solar system?",
    "answer": "Earth is the third planet from the Sun and has one natural satellite."
}

# Send request
response = requests.post(url, headers=headers, json=data)
result = response.json()

# Extract and analyze token-level results
print(f"Overall hallucination score: {result['hallucination_score']:.2f}")
print("\nToken-by-token analysis:")
print("-----------------------")

# Print each token with its prediction and probability
for token in result["tokens"]:
    # Create a simple visual indicator of hallucination likelihood
    indicator = "🔴" if token["pred"] == 1 else "🟢"
    
    # Print token with its hallucination probability
    print(f"{indicator} \"{token['token']}\" - Score: {token['prob']:.2f}")

# Optional: Save detailed results to file for further analysis
with open("token_analysis.json", "w") as f:
    json.dump(result["tokens"], f, indent=2)
    print("\nDetailed token analysis saved to token_analysis.json")
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://klaralabs.gitbook.io/klara-labs-docs/getting-started/advanced-token-level-analysis.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
