Skip to main content

Official Python SDK for Usefy - AI Cost Control & Budget Management

Project description

Usefy Python SDK

Official Python SDK for Usefy - AI Cost Control & Budget Management.

Installation

pip install usefy

Or install from source:

pip install git+https://github.com/sherlocq61/usefy.git#subdirectory=sdk/python

Quick Start

from usefy import Usefy

# Initialize client
client = Usefy(api_key="us_live_your_key_here")

# Use like OpenAI - model auto-detection included
response = client.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Hello!"}]
)

print(response["choices"][0]["message"]["content"])

Features

OpenAI-Compatible Interface

# GPT-4
response = client.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Explain quantum computing"}]
)

# Claude (auto-detected)
response = client.chat.completions.create(
    model="claude-3-opus",
    messages=[{"role": "user", "content": "Write a poem"}]
)

# Gemini
response = client.chat.completions.create(
    model="gemini-2.0-flash",
    provider="google",  # Explicit provider
    messages=[{"role": "user", "content": "Hello!"}]
)

Fail-Open Mode

For maximum reliability, enable fail-open mode. If Usefy is temporarily unavailable, the SDK will signal that you should fall back to direct provider calls:

client = Usefy(
    api_key="us_live_xxx",
    fail_open=True,
    fail_open_timeout=2.0  # 2 seconds
)

try:
    response = client.chat.completions.create(
        model="gpt-4",
        messages=[{"role": "user", "content": "Hello!"}]
    )
except UsefyTimeoutError:
    if client.is_fail_open_active:
        # Fall back to direct OpenAI call
        import openai
        response = openai.chat.completions.create(...)

Pre-flight Check

Check if a request would be allowed before making it:

check = client.check(
    provider="openai",
    model="gpt-4",
    estimated_tokens=1000
)

if check["allowed"]:
    # Proceed with request
    response = client.chat.completions.create(...)
else:
    print(f"Request would be blocked: {check['reason']}")

Usage Monitoring

usage = client.get_usage()

print(f"Requests: {usage['monthly_requests']['used']}/{usage['monthly_requests']['limit']}")
print(f"Plan: {usage['plan_name']}")

Configuration

Environment Variables

export USEFY_API_KEY="us_live_your_key"

Then:

client = Usefy()  # API key from env

Full Configuration

from usefy import Usefy, UsefyConfig

config = UsefyConfig(
    api_key="us_live_xxx",
    base_url="https://api.usefy.ai",
    timeout=30.0,
    max_retries=3,
    retry_delay=1.0,
    fail_open=False,
    fail_open_timeout=2.0,
    log_level="WARNING"
)

client = Usefy(config=config)

Supported Providers

Provider Models Auto-Detection
OpenAI gpt-4, gpt-3.5-turbo, o1, etc.
Anthropic claude-3-opus, claude-3-sonnet
Google gemini-2.0-flash, gemini-pro
Mistral mistral-large, mixtral
Cohere command-r, command
DeepSeek deepseek-chat
Groq llama-3.1-70b
Together various Manual
xAI grok-2

Error Handling

from usefy import (
    Usefy,
    UsefyError,
    UsefyAuthError,
    UsefyBudgetExceeded,
    UsefyRateLimited
)

try:
    response = client.chat.completions.create(...)
except UsefyAuthError:
    print("Invalid API key")
except UsefyBudgetExceeded as e:
    print(f"Budget exceeded: {e.budget_info}")
except UsefyRateLimited as e:
    print(f"Rate limited, retry after {e.retry_after}s")
except UsefyError as e:
    print(f"Error: {e.message}")

License

MIT License - see LICENSE for details.

Support

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

usefy-2.1.0.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

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

usefy-2.1.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file usefy-2.1.0.tar.gz.

File metadata

  • Download URL: usefy-2.1.0.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for usefy-2.1.0.tar.gz
Algorithm Hash digest
SHA256 d0f85438637500f084994cd1bbee88d79205f34487ac439fa32de28412706e24
MD5 37ed0508c9cb4c712b4eeb489c5a5e76
BLAKE2b-256 ea38e8732c4f5ba31eeee3ff8a7fd6a0f92f1d0396f37efbfc8afee612a980f4

See more details on using hashes here.

File details

Details for the file usefy-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: usefy-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for usefy-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9a97aa8cbedcb3547e931e74f49de50d562841b68a5d366e7323659c7a81a19e
MD5 b62085c4eb238866ace8b53239c211d2
BLAKE2b-256 633abab3717294bb340f2aab39a2d6bdd2b4f86208bed32702107648d790b4db

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