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

Quick start

from cortexos import Cortex

cx = Cortex(api_key="sk-...", agent_id="support-bot")

# Store a memory
mem = cx.remember(
    "User prefers email over Slack for all communications",
    importance=0.85,
    tags=["preferences", "communication"],
    metadata={"source": "conversation_123"},
)

# Semantic recall
results = cx.recall("how does the user want to be contacted?", top_k=5)
for r in results:
    print(f"[{r.score:.2f}] {r.memory.content}")

# EAS attribution — score which memories contributed to your LLM response
attr = cx.attribute(
    query="How should I contact the user?",
    response="Based on their preferences, contact them via email.",
    memory_ids=[mem.id],
)
print(attr.scores)  # {"mem_xxx": 0.91}

# Combined recall + attribution in one call
result = cx.recall_and_attribute(
    query="How should I reach the user?",
    response="Contact via email.",
    top_k=10,
)

Async usage

import asyncio
from cortexos import AsyncCortex

async def main():
    async with AsyncCortex(api_key="sk-...", agent_id="my-agent") as cx:
        mem = await cx.remember("User lives in Tokyo", importance=0.7)
        results = await cx.recall("where does the user live?")
        await cx.forget(mem.id)

asyncio.run(main())

API reference

Cortex / AsyncCortex

Method Description
remember(content, *, importance, tags, metadata, tier, ttl) Store a new memory
get(memory_id) Fetch a memory by ID
update(memory_id, *, importance, tags, metadata, tier) Update memory fields
forget(memory_id) Soft-delete a memory
list(*, limit, offset, tier, sort_by, order) Paginated memory listing
recall(query, *, top_k, min_score, tags) Semantic search
attribute(query, response, memory_ids) Run EAS attribution
recall_and_attribute(query, response, *, top_k, min_score) Recall + attribute

Types

  • Memoryid, content, agent_id, tier, importance, tags, metadata, retrieval_count, created_at
  • RecallResultmemory: Memory, score: float
  • Attributiontransaction_id, query, response, scores: dict[str, float]
  • RecallAndAttributeResultmemories: list[RecallResult], attribution: Attribution
  • Pageitems: list[Memory], total, offset, limit, has_more

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
MemoryNotFoundError Memory ID does not exist (404)
ValidationError Invalid request payload (422)
ServerError Unexpected 5xx from the server

Configuration

cx = Cortex(
    agent_id="my-agent",
    api_key="sk-...",           # Optional if server has no auth
    base_url="https://api.cortexa.ink",
    timeout=30.0,               # Per-request timeout (seconds)
    max_retries=3,              # Retries on 429/502/503/504
)

Running tests

# Unit tests (no server required)
pip install "cortexos[dev]"
pytest tests/test_client.py -v

# Integration tests (requires a running cortex-engine)
pytest tests/test_integration.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.0.tar.gz (47.6 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.0-py3-none-any.whl (49.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cortexos-0.2.0.tar.gz
  • Upload date:
  • Size: 47.6 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.0.tar.gz
Algorithm Hash digest
SHA256 ed68407366f6862452c9f874e054ea9beb6cda3b888343413ac9129ea8a129cc
MD5 be73b455a1965bdedca6fddcee5bd214
BLAKE2b-256 ea6a91471dfcd50021a296da71e87e4730ab7a686395d41f6a4bbfe0d900bc7a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cortexos-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 49.9 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5bbb9064998b87b98d98c4adf014fe6d61880a7d01e8c72dfb0ca46b8e1d3351
MD5 20c45402cc3c92c876a5dbfe1c7f53d7
BLAKE2b-256 647cb40c2af03395e43289195093e0b9c7e9d33a5c04fea8d4be69b9a3a36723

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