Skip to main content

Brokle Platform Python SDK for intelligent LLM routing, caching, and observability

Project description

Brokle Python SDK

Three integration patterns. One powerful platform.

The Brokle Python SDK provides intelligent routing across 250+ LLM providers, semantic caching (30-50% cost reduction), and comprehensive observability. Choose your integration level:

🎯 Three Integration Patterns

Pattern 1: Wrapper Functions Wrap existing SDK clients (OpenAI, Anthropic) for automatic observability and platform features.

Pattern 2: Universal Decorator Framework-agnostic @observe() decorator with automatic hierarchical tracing. Works with any AI library.

Pattern 3: Native SDK (Sync & Async) Full platform capabilities: intelligent routing, semantic caching, cost optimization. OpenAI-compatible interface with Brokle extensions.

Installation

pip install brokle

Setup

export BROKLE_API_KEY="bk_your_api_key_here"
export BROKLE_HOST="http://localhost:8080"

Quick Start

Pattern 1: Wrapper Functions

# Wrap existing SDK clients for automatic observability
from openai import OpenAI
from anthropic import Anthropic
from brokle import wrap_openai, wrap_anthropic

# OpenAI wrapper
openai_client = wrap_openai(
    OpenAI(api_key="sk-..."),
    tags=["production"],
    session_id="user_session_123"
)

# Anthropic wrapper
anthropic_client = wrap_anthropic(
    Anthropic(api_key="sk-ant-..."),
    tags=["claude", "analysis"]
)

response = openai_client.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Hello!"}]
)
# ✅ Automatic Brokle observability, routing, caching, optimization

Pattern 2: Universal Decorator

# Automatic hierarchical tracing with just @observe()
from brokle import observe
import openai

client = openai.OpenAI()

@observe(name="parent-workflow")
def main_workflow(data: str):
    # Parent span automatically created
    result1 = analyze_data(data)
    result2 = summarize_findings(result1)
    return f"Final result: {result1} -> {result2}"

@observe(name="data-analysis")
def analyze_data(data: str):
    # Child span automatically linked to parent
    response = client.chat.completions.create(
        model="gpt-4",
        messages=[{"role": "user", "content": f"Analyze: {data}"}]
    )
    return response.choices[0].message.content

@observe(name="summarization")
def summarize_findings(analysis: str):
    # Another child span automatically linked to parent
    response = client.chat.completions.create(
        model="gpt-4",
        messages=[{"role": "user", "content": f"Summarize: {analysis}"}]
    )
    return response.choices[0].message.content

# Automatic hierarchical tracing - no manual workflow management needed
result = main_workflow("User behavior data from Q4 2024")
# ✅ Complete span hierarchy: parent -> analyze_data + summarize_findings

Pattern 3: Native SDK

Sync Client:

from brokle import Brokle

# Context manager (recommended)
with Brokle(
    api_key="bk_...",
    host="http://localhost:8080"
) as client:
    response = client.chat.completions.create(
        model="gpt-4",
        messages=[{"role": "user", "content": "Hello!"}],
        routing_strategy="cost_optimized",  # Brokle extension
        cache_strategy="semantic"           # Brokle extension
    )
    print(f"Response: {response.choices[0].message.content}")

Async Client:

from brokle import AsyncBrokle
import asyncio

async def main():
    async with AsyncBrokle(
        api_key="bk_...",
    ) as client:
        response = await client.chat.completions.create(
            model="gpt-4",
            messages=[{"role": "user", "content": "Hello!"}],
            routing_strategy="cost_optimized",  # Smart routing
            cache_strategy="semantic",          # Semantic caching
            tags=["async", "production"]       # Analytics tags
        )
        print(f"Response: {response.choices[0].message.content}")

asyncio.run(main())

Why Choose Brokle?

  • 🚀 30-50% Cost Reduction: Intelligent routing and semantic caching
  • ⚡ <3ms Overhead: High-performance observability
  • 🔄 250+ Providers: Route across all major LLM providers
  • 📊 Complete Visibility: Real-time analytics and quality scoring
  • 🛠️ Three Patterns: Start simple, scale as needed

Next Steps

Examples

Check the examples/ directory:

Support


Simple. Powerful. Three patterns to fit your needs.

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

brokle-0.2.0.tar.gz (111.4 kB view details)

Uploaded Source

Built Distribution

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

brokle-0.2.0-py3-none-any.whl (104.8 kB view details)

Uploaded Python 3

File details

Details for the file brokle-0.2.0.tar.gz.

File metadata

  • Download URL: brokle-0.2.0.tar.gz
  • Upload date:
  • Size: 111.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.13

File hashes

Hashes for brokle-0.2.0.tar.gz
Algorithm Hash digest
SHA256 27c3c46ecc65896a54b0c76273285e3e17807d329f264eda751b75aa963a784e
MD5 5748fa09f83926d32220beca502b1839
BLAKE2b-256 d486bac335ba14633e6f0d311410e1d990eceb7ec4436be7a490d7556d860bba

See more details on using hashes here.

File details

Details for the file brokle-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: brokle-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 104.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.13

File hashes

Hashes for brokle-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 76a463ac3cc247b7e3d4724fd9f279024aa0a98268240f6abfd9f6c6be900727
MD5 057fa9de33df1de07e6a128b631285ac
BLAKE2b-256 aa8ace3c856612b33406be6e04e2b0a33b0c9023ed5fcd5feee0d255c3cb04bf

See more details on using hashes here.

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