Python SDK for the TokenSaver API: pipelines, chat sessions, and pricing estimates
Project description
TokenSaver SDK
Python client for the TokenSaver API (POST /pipelines/run, RAG, chat sessions, pricing).
Only HTTP transport and response normalization run in this package; all pipeline logic stays on the server.
Architecture (monorepo) : docs/ARCHITECTURE-SDK-TOKENSAVER.md
Installation
pip install tokensaver-sdk
Development inside the monorepo:
cd packages/sdk
python -m venv .venv && . .venv/bin/activate
pip install -e ".[dev]"
Configuration
api_key(required): TokenSaver API key (ts_...).base_url(optional): API base URL; defaulthttps://platform.tokensaver.fr/api/v1.provider_api_key(optional): LLM provider key (openai,anthropic, etc.) sent on everyask/run_pipelinewhen set here; takes precedence over organization keys stored in the database for that run only; never persisted by TokenSaver. You can also passprovider_api_key=on a single call.
from tokensaver_sdk import TokenSaver
ts = TokenSaver(api_key="ts_...")
# Local backend:
# ts = TokenSaver(api_key="ts_...", base_url="http://localhost:8000/api/v1")
# Default LLM key for all runs (optional):
# ts = TokenSaver(api_key="ts_...", provider_api_key="sk-...")
Pipeline calls (ask / run_pipeline)
ask() returns a RunResult (.text, .metrics, .trace, .context).
run_pipeline() returns the raw API JSON.
Module flags (off by default in the SDK, same as in the console):
use_cache, use_rag, use_compression, use_pii_filter, stream.
Advanced parameters (aligned with PipelineRunRequest / console) — passed through to the JSON body as-is:
| Parameter | Purpose |
|---|---|
temperature |
LLM temperature (0–2). |
rag_similarity_threshold |
RAG similarity threshold (0–1). |
cache_similarity_threshold |
Semantic cache similarity threshold (0–1). |
compression_level |
Compression level 1–5. |
rag_options |
Dict: document_ids, top_k, query_image_url. |
pii_options |
Dict: engine, strategy, confidence_threshold, entity_types, language, regex_fallback. |
context_layers |
Canonical shape (instructions, knowledge, interaction, token_budget). |
system_prompt, profile_context, workspace_instructions |
Legacy flat fields (if no context_layers). |
provider_api_key |
SDK: ephemeral LLM key for this run; overrides DB keys; not persisted. |
IDE helpers: from tokensaver_sdk import RagOptions, PiiOptions (TypedDict).
result = ts.ask(
"Your question",
provider="openai",
model="gpt-4o",
use_rag=True,
rag_similarity_threshold=0.55,
rag_options={"document_ids": ["uuid-doc"], "top_k": 8},
)
RAG (documents)
| Method | Role |
|---|---|
rag_list_documents() |
Lists PDFs in the workspace. |
rag_upload_document(path, …) |
Multipart upload (no wait). Raises ValidationError (RAG_FILE_NOT_FOUND) if the file is missing. |
rag_get_document(id) |
Status / metadata. |
rag_wait_document_ready(id, …) |
Wait for ingestion. |
rag_upload_and_wait(path, …) |
Upload + wait. |
rag_ensure_document(path, …) |
Reuses an already ingested file (same file name) or upload + wait. |
Minimal example with a question over a PDF:
doc = ts.rag_ensure_document("handbook.pdf")
ts.ask(
"What are the key points?",
provider="openai",
model="gpt-4o",
use_rag=True,
rag_options={"document_ids": [doc["document_id"]]},
)
Chat sessions
from tokensaver_sdk import HISTORY_NONE, HISTORY_LOCAL, HISTORY_SERVER
# Stateless (default for ask: history=HISTORY_NONE)
ts.ask("…", provider="openai", model="gpt-4o", history=HISTORY_NONE)
# Server-side persistence
session = ts.chat.session(history=HISTORY_SERVER, name="My chat")
session.ask("…", provider="openai", model="gpt-4o")
Cost estimate (no LLM call)
ts.estimate_cost(1200, 300, provider="openai", model="gpt-4o")
Errors
from tokensaver_sdk import ERROR_RAG_FILE_NOT_FOUND
from tokensaver_sdk.errors import (
TokenSaverError,
AuthenticationError,
ProviderKeyMissingError,
QuotaExceededError,
RateLimitError,
ValidationError,
ServerError,
TimeoutError,
)
HTTP errors map to these exceptions. For RAG uploads (rag_upload_document, rag_upload_and_wait, rag_ensure_document when a file is sent), a missing PDF path on the client raises ValidationError with code="RAG_FILE_NOT_FOUND" (compare to ERROR_RAG_FILE_NOT_FOUND); the raw payload includes "path" among other fields.
Tests & quality
pytest
ruff check src tests && ruff format src tests
Useful variables for integration tests: TOKENSAVER_API_KEY, base URL depending on your deployment.
Publishing to PyPI
- Version: bump
__version__insrc/tokensaver_sdk/__init__.py(single source of truth for the build). - Build & check:
python -m buildthentwine check dist/*(dev deps:pip install -e ".[dev]"). - Upload:
twine upload dist/*(PyPI username:__token__, password: your API token). Try TestPyPI first with--repository testpypi. - CI: after adding the
PYPI_API_TOKENrepository secret, pushing tagsdk-v0.1.0(same as__version__) triggers the Publish SDK to PyPI workflow.
Further reference
- TokenSaver console: API Reference page (examples, SDK method list).
- Architecture & decisions:
docs/ARCHITECTURE-SDK-TOKENSAVER.md.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tokensaver_sdk-0.1.1.tar.gz.
File metadata
- Download URL: tokensaver_sdk-0.1.1.tar.gz
- Upload date:
- Size: 15.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
49c61771ea7fb3c358b47d958d14179daf58e6d976eea06544d8f6895ebcdba7
|
|
| MD5 |
4388807261815b3efb9f79b922757735
|
|
| BLAKE2b-256 |
90a48cdd5eba5c8d75c1a5b309b139147b719cc506c11a97d90b1c52c13143f8
|
File details
Details for the file tokensaver_sdk-0.1.1-py3-none-any.whl.
File metadata
- Download URL: tokensaver_sdk-0.1.1-py3-none-any.whl
- Upload date:
- Size: 12.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
096c289176f31cfbbbe013d1830cd33e7e3d308f119de0d5677e6fbe3418646b
|
|
| MD5 |
80a80bba9ca8ca2b0f74e79121b7be17
|
|
| BLAKE2b-256 |
3055eacbec945372d308b19676ebb18dfd8af91e6f3dd92861b1fd825808f473
|