Skip to main content

TokenSpend Python

Official Python client for the TokenSpend API. It has no runtime dependencies. Requires Python 3.9+. Use a virtual environment.

Follow the canonical quickstart to set TOKENSPEND_API_KEY with hidden input. Use a TokenSpend rail key, never a provider or admin credential. Do not commit keys.

python3 -m pip install tokenspend
from tokenspend import TokenSpend

client = TokenSpend()
response = client.chat.completions.create(
    model="xai/grok-4.5",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello"}],
)
print(response.choices[0].message.content)
print(response.request_id)

Pass one stable key across retries of the same request:

response = client.chat.completions.create(
    model="xai/grok-4.5",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello"}],
    idempotency_key="job-42-generation",
)

Changing the request requires a new idempotency key. The safe TokenSpend request id is available as response.request_id, on a streaming iterator, and as error.request_id when a duplicate returns 409. Keys are scoped to workspace and route for 24 hours. Duplicates do not replay the response. Use a new key after a failed request if you want another attempt.

Discover canonical ids and protocol capabilities from the live registry:

models = client.models.list()
model = client.models.retrieve("zai/glm-5.2")

Set stream=True to iterate over server-sent chunks. client.messages.create(...) uses the Anthropic-compatible Messages API. See https://tokenspend.dev/docs/router.

Other canonical IDs include meta/muse-spark-1.2, thinking-machines/inkling, and moonshot/kimi-k3. Capabilities differ by model and format. Muse needs max_tokens=2048 or greater for its visible-output contract.

Requests are billable. Match response.request_id to the cost receipt in Router activity. Check stream errors and completion state before accepting output.

Request logging is off by default. Opt-in history is encrypted and expires after 30 days. Disabling logging stops new storage; existing history keeps its expiry. Capture is metadata-only. TokenSpend does not currently offer provider ZDR. See Trust and the agent compatibility review. SDK smoke tests do not verify agent sessions. Responses tools are unsupported.

The SDK never accepts a serving-host selector.

Download files

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

Source Distribution

tokenspend-0.1.2.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

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

tokenspend-0.1.2-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file tokenspend-0.1.2.tar.gz.

File metadata

  • Download URL: tokenspend-0.1.2.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.0

File hashes

Hashes for tokenspend-0.1.2.tar.gz
Algorithm Hash digest
SHA256 a8cc10ebff9c2cc552d3993df9b397f85fe8de3c26d07d91f9277de0ec7ef2f7
MD5 7652300bfccde969a5efc96657ab1526
BLAKE2b-256 5aae218ad13547c6c12f1dd47b69fd910f3758cb29ba133bea409a275d75ccd5

See more details on using hashes here.

File details

Details for the file tokenspend-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: tokenspend-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.0

File hashes

Hashes for tokenspend-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9272f986c31e1f24e7e4c6fa0cea850715e0893eb769aefe9d1fb8c6d74db291
MD5 81d6a9f0a6c24f39263dfc0660fbd594
BLAKE2b-256 978cbcf9415ed9352e0f6e470609852dd514b51cccd6b93bc120b244df934b17

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 files

0.1.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page