Skip to main content

Foundation for the Cendor stack: shared types, token counting, offline-first & refreshable prices, instrument(), event bus, OTel.

Project description

cendor-core

The shared foundation for the Cendor stack: canonical types, provider-aware token counting, an offline price table, one instrument() interception point, an in-process event bus, and OpenTelemetry GenAI emitters. Tiny on purpose — it's the blast radius for every other tool.

One instrument() call, every sibling tool observes the stream — no per-call wiring, offline by default.

PyPI license · usually installed transitively · import cendor.core

from cendor.core import tokens, prices, instrument, bus

# Count tokens and price a call — fully offline, no API key, no network:
n = tokens.count([{"role": "user", "content": "Summarize the attached report in 3 bullets."}],
                 model="claude-opus-4-8")
cost = prices.estimate("claude-opus-4-8", input_tokens=n, output_tokens=200)

# Instrument any client once; tools subscribe to the normalized event stream:
@bus.subscribe
def on_call(call):                   # normalized LLMCall with usage + cost
    print(call.provider, call.model, call.cost)

client = instrument(openai_or_anthropic_client)   # idempotent, additive · sync · async · streaming

Highlights

  • instrument() — wrap any client once: OpenAI (Chat Completions + Responses API) · Anthropic · AWS Bedrock · Google Gemini (google-genai + legacy google-generativeai) · Ollama, detected by shape; sync, async, and streaming; idempotent + additive. instrument_tool() does the same for tools.
  • Streaming is a context manager and an iterator — the streamed value supports both for chunk in stream / async for and with client…create(stream=True) as stream: / async with, matching the SDK's own stream and unbreaking frameworks (e.g. LangChain) that consume streams via with. Usage/cost finalize exactly once.
  • Event bussubscribe / emit; thread-safe within a process; one failing subscriber never starves another.
  • Interceptor seamadd_interceptor + Reroute / MISS powers replay (cassette) and reroute / block (tokenguard) without a second patch point.
  • Token counting, three tiers — exact ([tiktoken]) / BPE-estimate (o200k for Claude/Gemini) / offline heuristic; tokens.method(model) reports which is active; tokens.register() plugs in a precise counter.
  • Reasoning-token accountingUsage.reasoning_tokens breaks out a reasoning/thinking model's internal reasoning (OpenAI reasoning_tokens, Gemini thoughts_token_count), non-streaming and streaming. A subset of output_tokens, so cost is unchanged; Gemini's separately-reported thoughts are folded into the output total.
  • Offline-first, refreshable prices — bundled dated snapshot; estimate() -> Decimal Money (never float); optional refresh(source="litellm"|"openrouter"|"azure") from live no-auth sources, with age_days()/is_stale() staleness signals. Cached tokens are billed once (cached ⊆ input, normalized across providers), not at both the input and cached rate. A gateway-reported cost (e.g. OpenRouter's usage.cost) is preferred over the estimate and labeled cost_reported vs cost_estimated.
  • OpenTelemetry — emit gen_ai.* spans, or otel.ingest() a managed runtime's spans onto the bus. Structural protocols (Compressor / EvictionStrategy / Sink / Subscriber / Handle) let the tools interlock without coupling. Sink now has optional flush()/close() lifecycle methods (write-only sinks still valid).
  • LangChain / LangGraphcendor.core.langchain.CendorCallbackHandler (optional extra cendor-core[langchain]) is the SDK-aligned way to observe a framework: attach it as a callback to record usage + reasoning + tool calls + a root-run trace_id across a whole agent, with no client touch. Recording-only (enforcement stays on the instrument() seam). For direct-SDK agents, core.trace("run-id") sets the same ambient trace_id.

Install cendor-core[tiktoken] for exact OpenAI counts (heuristic fallback otherwise), [otel] to emit spans, or [langchain] for the LangChain/LangGraph callback handler. Provider SDKs are always optional extras.

A rendered architecture diagram lives in docs/core.md (GitHub renders Mermaid; PyPI shows code as text).

See docs/core.md · CHANGELOG. Part of the Cendor stack — github.com/cendorhq/cendor-libs. Powered by PowerAI Labs. Apache-2.0; provided "as is", without warranty — use at your own risk (LICENSE §7–8).

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

cendor_core-1.3.1.tar.gz (55.5 kB view details)

Uploaded Source

Built Distribution

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

cendor_core-1.3.1-py3-none-any.whl (39.9 kB view details)

Uploaded Python 3

File details

Details for the file cendor_core-1.3.1.tar.gz.

File metadata

  • Download URL: cendor_core-1.3.1.tar.gz
  • Upload date:
  • Size: 55.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for cendor_core-1.3.1.tar.gz
Algorithm Hash digest
SHA256 6761fedfe000299cfa0734c6c8f0273ffa5f88a0b47bbad748ae3ecad3a807d2
MD5 1c15aa9b450794df230f8008f3977f92
BLAKE2b-256 c482e25c1901763d86c951bd3a9c502638b11e81e1a092fa54c138c8dbaf9915

See more details on using hashes here.

Provenance

The following attestation bundles were made for cendor_core-1.3.1.tar.gz:

Publisher: release.yml on cendorhq/cendor-libs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cendor_core-1.3.1-py3-none-any.whl.

File metadata

  • Download URL: cendor_core-1.3.1-py3-none-any.whl
  • Upload date:
  • Size: 39.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for cendor_core-1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3b89c568f232bbe08a1e3e1abb620a1cd43c57632fd05543a3e4c84195e6a6e5
MD5 e6a9bb566e30b637e405187dc0e99777
BLAKE2b-256 d47a9cdb11950d70b6ea91363d9e3e8d2683f00c88e45a7c9d0cfe9a5092cade

See more details on using hashes here.

Provenance

The following attestation bundles were made for cendor_core-1.3.1-py3-none-any.whl:

Publisher: release.yml on cendorhq/cendor-libs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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