Skip to main content

Lightweight Python SDK for LLM cost tracking and attribution. Track every OpenAI, Anthropic, and Gemini call — cost, tokens, latency — by feature, team, and user.

Project description

CogsLayer

Know the true cost of every feature you ship.

CogsLayer tracks the direct costs of delivering your software — starting with AI. Attribute spend to features, teams, and models. Cut what's wasted.

Start free at cogslayer.com.

Two ways in

1. Python SDK (2 lines of code)

pip install cogslayer
import cogslayer
from cogslayer.openai import OpenAI

cogslayer.init(api_key="cl_live_xxx", service="my-api")

client = OpenAI()

@cogslayer.track(feature="chat", team="growth")
def ask(prompt: str) -> str:
    resp = client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": prompt}],
    )
    return resp.choices[0].message.content

Drop-in replacement clients for OpenAI, Anthropic, and Gemini. Every call is automatically tracked with cost, tokens, latency, and attribution context.

2. Provider integration (60 seconds, zero code)

Paste your OpenAI org key or Anthropic admin key. CogsLayer pulls actual billed costs — not token-based estimates — and syncs automatically.

Supported: OpenAI, Anthropic, Cursor, Claude Code.

Track. Understand. Save.

What you get
Track Every call tracked with cost, tokens, and latency. Attribute spend to features, teams, models, and services with @cogslayer.track. Agent sessions grouped automatically.
Understand Dashboard with spend by team, feature, model, provider. Actual billed costs from provider APIs. Estimate vs. billed drift detection. Budget alerts and forecasting.
Save Waste detection flags duplicates, retry bloat, and oversized models. Optimization rules with one-click apply. Realized savings ledger proves ROI.

Wrapped clients

OpenAI

from cogslayer.openai import OpenAI, AsyncOpenAI

client = OpenAI()           # exact same API as openai.OpenAI
async_client = AsyncOpenAI()  # exact same API as openai.AsyncOpenAI

Tracks chat.completions.create(), chat.completions.parse(), chat.completions.stream(), and responses.create().

OpenAI-compatible providers

from cogslayer.openai import OpenAI

groq = OpenAI(
    base_url="https://api.groq.com/openai/v1",
    api_key="gsk_...",
    provider="groq",
)

Works with Groq, Together, Fireworks, Ollama, or any OpenAI-compatible API.

Anthropic

from cogslayer.anthropic import Anthropic, AsyncAnthropic

client = Anthropic()

response = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello"}],
)

Tracks messages.create() and messages.stream(). Captures cache tokens.

Google Gemini

from cogslayer.gemini import Client

client = Client(api_key="...")

response = client.models.generate_content(
    model="gemini-2.0-flash",
    contents="Hello",
)

Tracks models.generate_content() (sync and async). Streaming supported.

What gets tracked

Metric Description
prompt_tokens Input tokens
completion_tokens Output tokens
reasoning_tokens Thinking tokens (o1, o3, o4-mini)
cached_tokens Prompt tokens served from cache
cost_usd Estimated cost from pricing registry
latency_ms Round-trip time
has_tool_calls Whether the response included tool calls
api_type chat / responses / messages / generate

Attribution

Every call is tagged with context from @cogslayer.track():

Field Description
feature Product feature (e.g. "chat", "code-review")
team Owning team (e.g. "engineering", "support")
user_id End-user identifier
service Service name (set in init())
environment "production" / "staging" / etc.

Streaming

Fully supported across all providers. CogsLayer injects stream_options for OpenAI and captures token counts from the final chunk automatically.

stream = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello"}],
    stream=True,
)
for chunk in stream:
    print(chunk.choices[0].delta.content or "", end="")

Custom pricing

cogslayer.register_model(
    "my-fine-tuned-gpt4",
    prompt_price_per_1k=0.006,
    completion_price_per_1k=0.012,
)

Running locally

python -m pip install -e ".[openai,dev]"
python -m pytest tests/ -q

Build the PyPI artifacts with:

python -m build
python -m twine check dist/*

Supported providers

Provider SDK wrapper Integration (billed costs)
OpenAI from cogslayer.openai import OpenAI Yes
Anthropic from cogslayer.anthropic import Anthropic Yes
Google Gemini from cogslayer.gemini import Client
Cursor Yes
Claude Code Yes
Any OpenAI-compatible OpenAI(base_url=..., provider="groq")

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

cogslayer-0.2.0.tar.gz (32.5 kB view details)

Uploaded Source

Built Distribution

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

cogslayer-0.2.0-py3-none-any.whl (48.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cogslayer-0.2.0.tar.gz
  • Upload date:
  • Size: 32.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for cogslayer-0.2.0.tar.gz
Algorithm Hash digest
SHA256 dc78bc6c1d2d13fb1c9fdfdf6bfab7baa952c77691b5bf276b920e6c3d386ca6
MD5 a9c6661db05148005a95c049462e6ba9
BLAKE2b-256 03959bfe1366cc49dfafa130d85b75f82623fddf335e55a837a189aad776ff87

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cogslayer-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 48.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for cogslayer-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ff3920a5372c1a365b41da682d442158c5b3b044f4ff761d00f3edbcd68d633d
MD5 2fee1c3f8bdd532035bd0a7602e1d233
BLAKE2b-256 0591e5d0170efad646c293f674838a64acbe2f14646ea54da5efe0357ed1a351

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