Skip to main content

koine-sdk

Python SDK for Koine — the HTTP gateway for Claude Code CLI.

Running the Gateway

docker run -d -p 3100:3100 \
  -e CLAUDE_CODE_GATEWAY_API_KEY=your-key \
  -e ANTHROPIC_API_KEY=your-anthropic-api-key \
  ghcr.io/pattern-zones-co/koine:latest

See Docker Deployment for version pinning and production setup.

Installation

uv pip install koine-sdk
# or: pip install koine-sdk

Quick Start

import asyncio
from koine_sdk import KoineConfig, create_koine

config = KoineConfig(
    base_url="http://localhost:3100",
    auth_key="your-api-key",
    timeout=300.0,
)

async def main():
    koine = create_koine(config)
    result = await koine.generate_text(prompt="Hello, how are you?")
    print(result.text)

asyncio.run(main())

Features

  • Text Generationkoine.generate_text() for simple prompts
  • Streamingkoine.stream_text() with async iterators
  • Structured Outputkoine.generate_object() with Pydantic schema validation
  • Tool Restrictionsallowed_tools parameter to limit CLI tool access
  • Streaming Structured Outputkoine.stream_object() with partial object streaming
  • Type Safety — Full type hints for all requests and responses
  • Error HandlingKoineError class with error codes

API

Client Factory

koine = create_koine(config)

Creates a client instance with the given configuration. The config is validated once at creation time.

Methods

Method Description
koine.generate_text(*, prompt, system?, session_id?, allowed_tools?) Generate text from a prompt
koine.stream_text(*, prompt, system?, session_id?, allowed_tools?) Stream text via Server-Sent Events
koine.generate_object(*, prompt, schema, system?, session_id?, allowed_tools?) Extract structured data using a Pydantic model
koine.stream_object(*, prompt, schema, system?, session_id?, allowed_tools?) Stream structured data via Server-Sent Events

Types

Type Description
KoineConfig Client configuration (base_url, auth_key, timeout, model)
KoineClient Client interface returned by create_koine()
GenerateTextResult Text generation response with usage stats
GenerateObjectResult[T] Object extraction response (generic over schema)
StreamTextResult Streaming result with async iterators and futures
StreamObjectResult[T] Streaming object result with partial_object_stream
KoineUsage Token usage information
KoineError Error class with code and raw_text

Error Handling & Retries

The SDK does not automatically retry failed requests. When the gateway returns 429 Too Many Requests (concurrency limit exceeded), your application should implement retry logic:

import asyncio
from koine_sdk import KoineError

async def generate_with_retry(prompt: str, max_retries: int = 3):
    for i in range(max_retries):
        try:
            return await koine.generate_text(prompt=prompt)
        except KoineError as e:
            if e.code == "CONCURRENCY_LIMIT_ERROR":
                await asyncio.sleep(1 * (i + 1))  # Exponential backoff
                continue
            raise
    raise Exception("Max retries exceeded")

Documentation

See the SDK Guide for:

  • Configuration options
  • Streaming examples
  • Structured output with Pydantic
  • Tool restrictions
  • Error handling
  • Multi-turn conversations

Examples

Runnable examples are available in the examples/ directory. Run from the SDK directory:

cd packages/sdks/python
uv pip install -e ".[dev]"
uv run python examples/hello.py           # Basic text generation
uv run python examples/extract_recipe.py  # Structured output with Pydantic
uv run python examples/stream.py          # Real-time streaming
uv run python examples/stream_object.py   # Streaming structured output
uv run python examples/conversation.py    # Multi-turn sessions

License

Dual-licensed under AGPL-3.0 or commercial license.

Release files for koine-sdk 2.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for koine-sdk 2.3.0
File Size Uploaded
koine_sdk-2.3.0.tar.gz 47.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for koine-sdk 2.3.0
File Interpreter ABI Platform
koine_sdk-2.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 65.7 kB

Release files / koine_sdk-2.3.0.tar.gz

Download URL koine_sdk-2.3.0.tar.gz
Size 47.1 kB
Tags Source
SHA-256 checksum
How to use checksums
ba917a060bcda0a0299397aa5f52f6c5d06475ccaa85f3b6e89b15317afb1362
BLAKE2b-256 checksum
How to use checksums
14109e45ac35498298757888b7051b4591891cc95b7b634c2c9d389c387027cf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 27, 2025.

Transparency log

Release files / koine_sdk-2.3.0-py3-none-any.whl

Download URL koine_sdk-2.3.0-py3-none-any.whl
Size 18.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
422de0d85d5ab3d862a679c2d1eb9433f30d480fcc757e0f181d61a99d66f2d1
BLAKE2b-256 checksum
How to use checksums
0735ea78334874d7a88208fd2d02030cecebe544e26dd4ae0235b71f350c11a5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 27, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

2.3.0 This release

2 release files

2.2.0

2 release files

2.1.0

2 release files

2.0.4

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release 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