Skip to main content

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

  • CheckResulthallucination_index, total_claims, grounded_count, hallucinated_count, claims, passed
  • ClaimResulttext, grounded, verdict, reason, source_quote, confidence
  • GateResultgrounded, 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


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.2.tar.gz (42.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cortexos-0.2.2-py3-none-any.whl (46.7 kB view details)

Uploaded Python 3

File details

Details for the file cortexos-0.2.2.tar.gz.

File metadata

  • Download URL: cortexos-0.2.2.tar.gz
  • Upload date:
  • Size: 42.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for cortexos-0.2.2.tar.gz
Algorithm Hash digest
SHA256 ed7a9f601d9005a628805bed2928d03d76836e40a37cba777a130157b7a9fa47
MD5 468606e183204dcb643defa5ad577487
BLAKE2b-256 fa9ac23a0bf0bff4cd08a61bb92a16d7863286182c8c28d572bbfe3fa1159df1

See more details on using hashes here.

File details

Details for the file cortexos-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: cortexos-0.2.2-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

Hashes for cortexos-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7ecca33219f6a9708c37cd82516350abdba7e11ca1f1998ff19b300fefd88580
MD5 05956aa2bb5a9166c18fdb3420ef5c4c
BLAKE2b-256 6a194396c4555e2a745fcb6d2ccf4a35527687700e27d95179a524543ae5a107

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page