Skip to main content

peufy-cortex-sdk · Python

Observable cognition infrastructure for AI systems — Python SDK.

Zero required dependencies. Adapters for OpenAI, LangChain, and OpenTelemetry are optional.

Install

pip install peufy-cortex-sdk                  # core only
pip install "peufy-cortex-sdk[openai]"        # + OpenAI adapter
pip install "peufy-cortex-sdk[langchain]"     # + LangChain adapter
pip install "peufy-cortex-sdk[opentelemetry]" # + OpenTelemetry GenAI bridge
pip install "peufy-cortex-sdk[all]"           # everything

Quick start

from cortex import Cortex

cortex = Cortex(
    project_key = "cortex_sk_...",
    endpoint    = "https://cortex.peufy.com",   # or http://localhost:3000
)

session = cortex.session(channel="api")
turn    = session.turn()

turn.input(text="What's the weather in Mumbai?", modality="text")

inf = turn.inference(model="gpt-4o-mini", provider="openai")
try:
    # ... call your LLM ...
    response = openai_client.chat.completions.create(...)
    inf.complete(
        latency_ms = 430,
        tokens     = {"input": 312, "output": 64},
    )
except Exception as e:
    inf.fail(e)
    raise

turn.output(text="It is 34°C and humid.", modality="text")
turn.end()
session.end()

cortex.shutdown()   # flush before process exit

Tool calls

turn.tool(
    name       = "weather_api",
    arguments  = {"city": "Mumbai"},
    result     = {"temp_c": 34, "condition": "humid"},
    status     = "success",
    latency_ms = 180,
)

If status="failure", a linked error.raised event is emitted automatically.

Explicit errors

turn.error(message="Timeout waiting for DB", severity="warning", source="system")

Context managers

with cortex.session(channel="api") as session:
    with session.turn() as turn:
        turn.input(text="Hello", modality="text")
        # ... turn.end() called automatically on __exit__

OpenAI auto-instrumentation

import openai
from cortex.adapters.openai import instrument_openai

client         = openai.OpenAI(api_key="...")
tracked_client = instrument_openai(client, cortex, channel="api")

# Use tracked_client exactly like openai.OpenAI — everything is automatic.
response = tracked_client.chat.completions.create(
    model    = "gpt-4o-mini",
    messages = [{"role": "user", "content": "Hello"}],
)

LangChain auto-instrumentation

from cortex.adapters.langchain import CortexCallbackHandler

handler = CortexCallbackHandler(cortex, channel="agent")

result = chain.invoke(
    {"input": "Summarise this document"},
    config={"callbacks": [handler]},
)

OpenTelemetry GenAI bridge

If your stack already emits OTel traces with GenAI instrumentation (OpenAI/ Anthropic auto-instrumentation, OpenLLMetry, OpenInference, or a hand-rolled tracer following the OTel GenAI semantic conventions), attach this SpanProcessor to your TracerProvider for zero-code-change instrumentation:

from opentelemetry.sdk.trace import TracerProvider
from cortex.adapters.otel import CortexOtelSpanProcessor

provider = TracerProvider()
provider.add_span_processor(CortexOtelSpanProcessor(cortex, channel="api"))

Mapping model: one OTel trace → one Cortex Session (closes when the trace's root span ends); one chat / text_completion span → one Cortex Turn; one execute_tool span → turn.tool() on its parent turn. Both the official semconv (span events gen_ai.content.prompt / .completion) and the Traceloop/OpenLLMetry flattened-attribute convention (gen_ai.prompt.{i}.content) are handled. create_agent / invoke_agent spans and non-GenAI spans are not yet mapped — v1 limitation.

Shutdown

Always call cortex.shutdown() before sys.exit() to flush buffered events.

For serverless / Lambda, call cortex.flush() at the end of each handler instead.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

peufy_cortex_sdk-0.1.1.tar.gz (28.5 kB view details)

Uploaded Source

Built Distribution

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

peufy_cortex_sdk-0.1.1-py3-none-any.whl (34.2 kB view details)

Uploaded Python 3

File details

Details for the file peufy_cortex_sdk-0.1.1.tar.gz.

File metadata

  • Download URL: peufy_cortex_sdk-0.1.1.tar.gz
  • Upload date:
  • Size: 28.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.10.11

File hashes

Hashes for peufy_cortex_sdk-0.1.1.tar.gz
Algorithm Hash digest
SHA256 a55861325b836bca56f92eac8bcf97ec29ca0c1b7c45880f2c2bdf75949615bc
MD5 fd4a852295042ee5c0d81f71e640a131
BLAKE2b-256 432e6e45fad5facf15058c65abc5d2f26870699b09e99682e73c49aac0871b41

See more details on using hashes here.

File details

Details for the file peufy_cortex_sdk-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for peufy_cortex_sdk-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b77c6b83c0a06abc71f9101c237d37a830f4734a384d1505f8bf70ade02ab26d
MD5 8c0ffcbc8aa41ab6fa23ec60933d633a
BLAKE2b-256 7a66b9e92dc264dd48b871e4c47b1514d76c716b37923af6cdc90796d10f6c7f

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 Sentry Error logging StatusPage Status page