Skip to content
AI-SpendOpsPublic

About

Runnable Python, TypeScript and .NET examples for tracking LLM cost across 15+ providers with the AI SpendOps proxy.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AI SpendOps — LLM cost tracking examples (Python, TypeScript, .NET)

Drop-in LLM cost tracking and FinOps for AI teams. Point your existing OpenAI, Anthropic, Google, or Groq SDK at the AI SpendOps proxy, add one header, and get per-request spend, token, cache, and reasoning-token telemetry across all your providers — without rewriting a line of application code.

Website Docs License

AI SpendOps is a BYO-key observability proxy that sits transparently in front of 16+ LLM providers. You keep your own provider API keys, you keep using the official SDKs, and every request flows through a Cloudflare edge worker that captures usage, cost, and latency for a unified FinOps dashboard. This repository contains runnable example projects in Python, TypeScript / Node.js, and C# / .NET 8 showing exactly how to wire up the drop-in integration for each stack.


30 seconds to drop-in

Here is a stock Python call to OpenAI:

from openai import OpenAI

client = OpenAI(api_key="sk-...")
resp = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Hello"}],
)

And here is the same call routed through AI SpendOps for full LLM cost tracking — the three lines that change are marked:

from openai import OpenAI

client = OpenAI(
    api_key="sk-...",                                            # your own OpenAI key, unchanged
    base_url="https://proxy.aispendops.com/v1/openai/v1",        # +++ 1. point at the proxy
    default_headers={"X-ASO-API-Key": "aso_k_xxx.yyy"},          # +++ 2. add your AI SpendOps key
)
resp = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Hello"}],
)

That's the whole integration. No SDK wrapper, no code rewrite, no lock-in. Every call is now captured in your spend dashboard with prompt / completion / cache / reasoning token breakdowns and per-tenant cost attribution.


Architecture

AI SpendOps architecture: your app calls the vanilla provider SDK, which points at the AI SpendOps proxy, which forwards to the provider and emits usage events to a ClickHouse-backed dashboard

Your application keeps using the vanilla provider SDK. Requests are forwarded byte-for-byte to the upstream provider, so responses (including streaming) are identical. In parallel, the proxy captures usage events asynchronously via ctx.waitUntil — zero latency overhead on the response path — and enriches them with cost, cache, and reasoning token breakdowns before they land in ClickHouse.


Examples by stack and provider

Every cell links to a complete, runnable example project.

Provider Python TypeScript .NET 8
OpenAI (chat) python/openai_chat.py typescript/src/openai-chat.ts dotnet/OpenAIChat
OpenAI (streaming + usage) python/openai_streaming.py typescript/src/openai-streaming.ts dotnet/OpenAIStreaming
Anthropic (chat) python/anthropic_chat.py typescript/src/anthropic-chat.ts dotnet/AnthropicChat
Anthropic (streaming) python/anthropic_streaming.py typescript/src/anthropic-streaming.ts — (same pattern as AnthropicChat)
Groq (OpenAI-compatible) python/groq_chat.py typescript/src/groq-chat.ts dotnet/GroqChat
Google Gemini python/google_chat.py typescript/src/google-chat.ts dotnet/GoogleChat

Works with every provider AI SpendOps supports

The pattern is identical for every supported provider — you only change the base_url (and, for OpenAI-compatible 3rd parties, keep using the OpenAI SDK). The Groq example in this repo is the canonical template for all OpenAI-compatible endpoints; the Google example is the template for native REST providers.

Provider SDK pattern Proxy base URL
OpenAI openai SDK https://proxy.aispendops.com/v1/openai/v1
Anthropic anthropic SDK https://proxy.aispendops.com/v1/anthropic
Google Gemini google-genai SDK https://proxy.aispendops.com/v1/google-ai-studio
Groq openai SDK https://proxy.aispendops.com/v1/groq/v1
OpenRouter openai SDK https://proxy.aispendops.com/v1/openrouter/api/v1
xAI (Grok) openai SDK https://proxy.aispendops.com/v1/xai/v1
DeepSeek openai SDK https://proxy.aispendops.com/v1/deepseek/v1
Cerebras openai SDK https://proxy.aispendops.com/v1/cerebras/v1
Mistral openai SDK https://proxy.aispendops.com/v1/mistral/v1
DeepInfra openai SDK https://proxy.aispendops.com/v1/deepinfra/v1
Fireworks openai SDK https://proxy.aispendops.com/v1/fireworks/inference/v1
Nebius openai SDK https://proxy.aispendops.com/v1/nebius/v1
Novita openai SDK https://proxy.aispendops.com/v1/novita/v3/openai
Perplexity openai SDK https://proxy.aispendops.com/v1/perplexity
z.ai openai SDK https://proxy.aispendops.com/v1/zai
Vercel AI Gateway openai SDK https://proxy.aispendops.com/v1/vercel/v1

See the URL format reference for the canonical list.


Why route through AI SpendOps?

  • Per-key spend limits — set hard caps and soft alerts on each API key across every provider.
  • Cache, reasoning, and audio token breakdowns — we split out cache_read, cache_write, reasoning_tokens, and modality tokens so your cost dashboard matches the bill.
  • Model and provider allow-lists — restrict which models an API key can call, enforced at the edge.
  • FOCUS-compatible exports — usage data conforms to the FinOps Open Cost & Usage Specification so it slots into existing FinOps tooling.
  • Multi-provider in one dashboard — 16+ LLM providers unified into a single FinOps view for AI.
  • Zero vendor lock-in — you keep your own provider keys, you keep using the official SDKs, you can turn the proxy off in one config line.

Full feature docs: docs.aispendops.com. Plans, pricing, and what the dashboard actually looks like: www.aispendops.com.


Quickstarts

Copy .env.example to .env and fill in your AI SpendOps key plus whichever provider keys you want to test against.

cp .env.example .env
Python — uv sync && uv run python openai_chat.py
cd python
uv sync                              # or: pip install -e .
uv run python openai_chat.py

Expected output:

--- OpenAI via AI SpendOps proxy ---
Hello! How can I help you today?
---
prompt_tokens     : 9
completion_tokens : 9
total_tokens      : 18

See python/README.md for the full list of examples and troubleshooting.

TypeScript / Node.js — pnpm install && pnpm tsx src/openai-chat.ts
cd typescript
pnpm install                         # or: npm install
pnpm tsx src/openai-chat.ts

Expected output:

--- OpenAI via AI SpendOps proxy ---
Hello! How can I help you today?
---
prompt_tokens     : 9
completion_tokens : 9
total_tokens      : 18

See typescript/README.md for the full list of examples.

C# / .NET 8 — dotnet run --project OpenAIChat
cd dotnet
dotnet restore
dotnet run --project OpenAIChat

Expected output:

--- OpenAI via AI SpendOps proxy ---
Hello! How can I help you today?
---
prompt_tokens     : 9
completion_tokens : 9
total_tokens      : 18

See dotnet/README.md for the full list of example projects.


Learn more


Start tracking your AI spend

These examples need one AI SpendOps key (aso_k_...) to run. Create an account, generate a key, and every request in this repo starts reporting cost, tokens, and latency to your dashboard.

Get started at www.aispendops.com · Pricing · How it works · Migrating from Helicone


Contributing

Missing a stack you care about (Go, Rust, Java, PHP, Ruby, Elixir)? PRs welcome — open an issue describing the stack and provider combination and we'll help you scaffold it in the same four-block structure used by the existing examples.


License

MIT — see LICENSE.

About

Runnable Python, TypeScript and .NET examples for tracking LLM cost across 15+ providers with the AI SpendOps proxy.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages