Skip to main content

Protective wrappers around paid API clients with quotas & duplicate detection

Project description

garde-fou (Python)

PyPI version Python Tests Python versions License: MIT Downloads

garde-fou is a lightweight guard for protecting against accidental over-usage of paid API calls. It provides call counting and duplicate detection to help you avoid unexpected API bills.

Features

  • Call counting - Set maximum number of calls and get warnings or exceptions when exceeded
  • Duplicate detection - Detect and handle repeated identical API calls
  • Flexible violation handling - Choose to warn, raise exceptions, or use custom handlers
  • Configuration support - Load settings from JSON/YAML files or set programmatically
  • Async support - Works with both synchronous and asynchronous functions

Installation

pip install garde-fou

Quick Start

from gardefou import GardeFou

# Protect any function with call limits
guard = GardeFou(max_calls=5, on_violation_max_calls="warn")

# Instead of: result = expensive_api_call("query")
# Use: result = guard(expensive_api_call, "query")
result = guard(your_api_function, "your", "arguments")

Usage Examples

Basic Call Limiting

from gardefou import GardeFou, QuotaExceededError

# Create a guard with a 3-call limit
guard = GardeFou(max_calls=3, on_violation_max_calls="raise")

try:
    for i in range(5):
        result = guard(api_call, f"query {i}")
except QuotaExceededError:
    print("Call limit exceeded!")

Duplicate Call Detection

# Warn on duplicate calls
guard = GardeFou(on_violation_duplicate_call="warn")

guard(api_call, "hello")  # First call - OK
guard(api_call, "hello")  # Duplicate - Warning logged
guard(api_call, "world")  # Different call - OK

Using Profiles

from gardefou import Profile

# Create a profile with multiple rules
profile = Profile(
    max_calls=10,
    on_violation_max_calls="raise",
    on_violation_duplicate_call="warn"
)

guard = GardeFou(profile=profile)

Configuration Files

# Load from JSON/YAML file
profile = Profile(config="gardefou.config.json")
guard = GardeFou(profile=profile)

# Or pass config as dict
config = {"max_calls": 5, "on_violation_max_calls": "warn"}
profile = Profile(config=config)

Configuration Options

  • max_calls: Maximum number of calls allowed (-1 for unlimited)
  • on_violation_max_calls: Handler when call limit exceeded ("warn", "raise", or callable)
  • on_violation_duplicate_call: Handler for duplicate calls ("warn", "raise", or callable)
  • on_violation: Default handler for all violations

How It Works

garde-fou works by wrapping your function calls. Instead of calling your API function directly, you call it through the guard:

# Before
result = openai.chat.completions.create(messages=[...])

# After  
guard = GardeFou(max_calls=10)
result = guard(openai.chat.completions.create, messages=[...])

The guard tracks calls and enforces your configured rules before executing the actual function.

Contributing

This is part of the multi-language garde-fou toolkit. See the main repository for contributing guidelines.

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

garde_fou-0.1.11.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

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

garde_fou-0.1.11-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file garde_fou-0.1.11.tar.gz.

File metadata

  • Download URL: garde_fou-0.1.11.tar.gz
  • Upload date:
  • Size: 9.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for garde_fou-0.1.11.tar.gz
Algorithm Hash digest
SHA256 148c683ea12d0bf6e1ff705e806c946033a1a2ab4afb389d6e080ce8a4e139a7
MD5 a7fc564ee56278f922f5ac14c5d81dfd
BLAKE2b-256 60f2e3b772d358396720072be7691e7dfeb4dab45aae71c01559fc0b7ab6c707

See more details on using hashes here.

File details

Details for the file garde_fou-0.1.11-py3-none-any.whl.

File metadata

  • Download URL: garde_fou-0.1.11-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for garde_fou-0.1.11-py3-none-any.whl
Algorithm Hash digest
SHA256 69e91e20fbce5ff19dc047a3161055af789df0ca91b808f01201a3e82bd50b90
MD5 b318456acc6006ac898d109f1261c1fe
BLAKE2b-256 24dae743276d2eda0954492be2154392fb7546f83a3f7490fc5fe8dae085afd3

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