Skip to main content

Python SDK for Koine gateway

Project description

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
  • 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?) Generate text from a prompt
koine.stream_text(*, prompt, system?, session_id?) Stream text via Server-Sent Events
koine.generate_object(*, prompt, schema, system?, session_id?) Extract structured data using a Pydantic model

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
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
  • 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/conversation.py    # Multi-turn sessions

License

Dual-licensed under AGPL-3.0 or commercial license.

Project details


Download files

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

Source Distribution

koine_sdk-2.2.0.tar.gz (46.8 kB view details)

Uploaded Source

Built Distribution

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

koine_sdk-2.2.0-py3-none-any.whl (18.3 kB view details)

Uploaded Python 3

File details

Details for the file koine_sdk-2.2.0.tar.gz.

File metadata

  • Download URL: koine_sdk-2.2.0.tar.gz
  • Upload date:
  • Size: 46.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for koine_sdk-2.2.0.tar.gz
Algorithm Hash digest
SHA256 9e004861d32e356c39713052ae2ee67ab28c4ada9da53e1839760ea5f5f075d9
MD5 2a7ff6411209092949142031b90b542a
BLAKE2b-256 36e376ce5a729f480354502f1a5baf99b091bf6048443442872023699d33c8f1

See more details on using hashes here.

Provenance

The following attestation bundles were made for koine_sdk-2.2.0.tar.gz:

Publisher: release.yml on pattern-zones-co/koine

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file koine_sdk-2.2.0-py3-none-any.whl.

File metadata

  • Download URL: koine_sdk-2.2.0-py3-none-any.whl
  • Upload date:
  • Size: 18.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for koine_sdk-2.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6ba09e21c9a483d7f4e93be02cec5a3b0622d9c9b8db914e9a657f297b302785
MD5 3c0a2c2de7682313a153e3a1f1405900
BLAKE2b-256 7489796ed2d3d89a20afe28e3cd63e195efff332d1bb8897c14f75851d1f7c79

See more details on using hashes here.

Provenance

The following attestation bundles were made for koine_sdk-2.2.0-py3-none-any.whl:

Publisher: release.yml on pattern-zones-co/koine

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page