Skip to content

SuperInstance/cocapn-py

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

cocapn-py — Python SDK

PyPI version SuperInstance

One API key, any AI model, see what it costs. Python edition.

What This Gives You

  • One API key — route to DeepSeek, GPT-4o, Claude, Gemini, and more through a single endpoint
  • Cost transparency — every response includes exact cost, token counts, and provider
  • Streamingchat_stream() yields text chunks as they arrive
  • Model catalog — list available models with per-token pricing
  • Usage stats — query your spend by day, week, or month
  • Type-hinted — full type annotations, dataclass responses

Quick Start

pip install cocapn
from cocapn import Cocapn

client = Cocapn()  # uses COCAPN_API_KEY env var

response = client.chat(
    "Explain transformers in one paragraph",
    model="deepseek-chat",
    system="You are a concise teacher."
)

print(response.text)      # "Transformers are..."
print(response.cost)      # 0.000042
print(response.tokens)    # TokenCount(in_=15, out=47)
print(response.provider)  # "deepseek"

Streaming

for chunk in client.chat_stream("Tell me a story", model="gpt-4o"):
    print(chunk, end="", flush=True)

List Models

for m in client.models():
    print(f"{m.id:30s} ${m.cost_in}/in  ${m.cost_out}/out")

Usage Stats

usage = client.usage("week")

API Reference

Cocapn(api_key=None, base_url=None)

Param Default Description
api_key COCAPN_API_KEY env Your Cocapn API key
base_url https://cocapn.ai API base URL

chat(message, *, model="deepseek-chat", system=None, max_tokens=4096, temperature=None, history=None) → ChatResponse

Returns ChatResponse(text, cost, tokens: TokenCount, model, provider, raw).

chat_stream(message, ...) → Iterator[str]

Yields text chunks.

models() → list[ModelInfo]

usage(period="day") → UsageReport

How It Fits

The Python counterpart to cocapn-sdk (Node.js). Both talk to the same API gateway.

Testing

pip install pytest pytest-asyncio
pytest tests/

Installation

pip install cocapn

Requires Python 3.9+. Uses httpx for HTTP. MIT license.

About

cocapn Python SDK — pip install cocapn — one API key, any AI model

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Python 100.0%