CortexOS Python SDK — hallucination detection for LLM agents
Project description
CortexOS Python SDK
The official Python SDK for Cortexa — hallucination detection and verification for LLM agents.
Quickstart
pip install cortexos
import cortexos
cortexos.configure(api_key="your-key")
result = cortexos.check(
response="The return window is 30 days",
sources=["Return policy: 30-day window for all items."]
)
print(f"Hallucination Index: {result.hallucination_index}")
# → Hallucination Index: 0.0
Get an API key at cortexa.ink
Installation
pip install cortexos
Usage
Simple API
import cortexos
# Configure once
cortexos.configure(api_key="cx-...")
# Check an LLM response against source documents
result = cortexos.check(
response="The product ships in 2-3 business days",
sources=["Shipping: 2-3 business day delivery for all orders."]
)
print(result.hallucination_index) # 0.0 = fully grounded
print(result.passed) # True (HI < 0.3)
# Gate a memory before storing
gate = cortexos.gate(
memory="Revenue grew 500% last quarter",
sources=["Revenue grew 10% in Q4."]
)
print(gate.grounded) # False
print(gate.flagged_claims) # [{"text": "...", "verdict": "NUM_MISMATCH"}]
Client API
from cortexos import Cortex
cx = Cortex(api_key="cx-...", base_url="https://api.cortexa.ink")
result = cx.check(
response="The return window is 30 days",
sources=["Return policy: 30-day window for all items."],
)
print(result.hallucination_index)
Async usage
import asyncio
from cortexos import AsyncCortex
async def main():
async with AsyncCortex(api_key="cx-...") as cx:
result = await cx.check(
response="The return window is 30 days",
sources=["Return policy: 30-day window for all items."],
)
print(result.hallucination_index)
asyncio.run(main())
API reference
Cortex / AsyncCortex
| Method | Description |
|---|---|
check(response, sources) |
Full hallucination check with per-claim verdicts |
gate(memory, sources) |
Gate check: should this memory be stored? |
health() |
Check server health |
Top-level functions
| Function | Description |
|---|---|
cortexos.check(response, sources) |
One-liner hallucination check |
cortexos.gate(memory, sources) |
One-liner gate check |
cortexos.configure(api_key, base_url) |
Set module-level defaults |
Types
CheckResult—hallucination_index,total_claims,grounded_count,hallucinated_count,claims,passedClaimResult—text,grounded,verdict,reason,source_quote,confidenceGateResult—grounded,hallucination_index,flagged_claims
Errors
| Exception | When |
|---|---|
CortexError |
Base class for all SDK errors |
AuthError |
Invalid or missing API key (401/403) |
RateLimitError |
Too many requests (429); has .retry_after |
ServerError |
Unexpected 5xx from the server |
TUI Monitor
pip install "cortexos[tui]"
cortexos monitor --api-key cx-... --url https://api.cortexa.ink
Configuration
cx = Cortex(
api_key="cx-...",
base_url="https://api.cortexa.ink",
timeout=30.0,
max_retries=3,
)
Or use environment variables:
export CORTEX_API_KEY=cx-...
export CORTEX_URL=https://api.cortexa.ink
Running tests
pip install "cortexos[dev]"
pytest tests/test_client.py -v
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
cortexos-0.2.1.tar.gz
(42.0 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
cortexos-0.2.1-py3-none-any.whl
(46.7 kB
view details)
File details
Details for the file cortexos-0.2.1.tar.gz.
File metadata
- Download URL: cortexos-0.2.1.tar.gz
- Upload date:
- Size: 42.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1292545645a4cd3ba8176f12884a8d970be98cdc1846de98d00314c964427b32
|
|
| MD5 |
99f0b8a5357b9c1938c8793672b79084
|
|
| BLAKE2b-256 |
94b48694300fbf160010de1b02d247b37441eadd1bebebec26b8bf5b9858fd41
|
File details
Details for the file cortexos-0.2.1-py3-none-any.whl.
File metadata
- Download URL: cortexos-0.2.1-py3-none-any.whl
- Upload date:
- Size: 46.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bed2c638f20cbbbdeaee956803da58ea0a0673b1123954cf967b71a7049dd64e
|
|
| MD5 |
e1e8e1918ac0aadfda7fd2073a1326af
|
|
| BLAKE2b-256 |
fdfaad2716dfe057b42ef3c801eab17619fb4c4f216669e232a396f862eab079
|