Skip to main content

Multi-provider AI dispatcher with response caching and error handling.

Project description

cross-ai-core

Multi-provider AI dispatcher with MD5-keyed response caching and unified error handling.

Supports Anthropic, xAI, OpenAI, Google Gemini, and Perplexity through a single consistent interface.

Requirements

  • Python 3.10 or newer (3.11 recommended for development)
  • No upper version limit — tested on 3.10–3.13

Install

pip install cross-ai-core

Dependencies

Package Version Purpose
anthropic ≥0.84.0 Anthropic / Claude API client
google-genai ≥1.65.0 Google Gemini API client
openai ≥1.70.0 OpenAI and xAI (Grok) API client
requests ≥2.32.4 HTTP for Perplexity API

Quick start

from dotenv import load_dotenv
load_dotenv("~/.crossenv")          # your app loads keys; the library reads os.environ

from cross_ai_core import process_prompt, get_content, get_default_ai

provider = get_default_ai()         # reads DEFAULT_AI from env, falls back to "xai"
result   = process_prompt(provider, "Explain transformer attention in 3 sentences.",
                          verbose=False, use_cache=True)
print(get_content(provider, result.response))

Configuration (environment variables)

Variable Default Purpose
DEFAULT_AI xai Default provider when none is specified
XAI_API_KEY xAI / Grok API key
ANTHROPIC_API_KEY Anthropic / Claude API key
OPENAI_API_KEY OpenAI API key
GEMINI_API_KEY Google Gemini API key
PERPLEXITY_API_KEY Perplexity API key
CROSS_API_CACHE_DIR ~/.cross_api_cache/ Response cache directory
CROSS_NO_CACHE Set to 1 to disable caching globally

The library only reads from os.environ — it never calls load_dotenv() itself.
Load your .env or ~/.crossenv before importing.

Caching

Responses are cached by MD5 hash of the request payload in ~/.cross_api_cache/.
The cache is safe to delete at any time.

# Bypass cache for one call
result = process_prompt(provider, prompt, verbose=False, use_cache=False)

# Check if a response was served from cache
if result.was_cached:
    print("from cache")

Adding a provider

  1. Create cross_ai_core/ai_<name>.py implementing BaseAIHandler (get_payload, get_client, get_cached_response, get_model, get_make, get_content, put_content, get_data_content, get_title, get_usage).
  2. Register in cross_ai_core/ai_handler.py: add to AI_HANDLER_REGISTRY and AI_LIST.

License

MIT

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

cross_ai_core-0.2.0.tar.gz (21.4 kB view details)

Uploaded Source

Built Distribution

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

cross_ai_core-0.2.0-py3-none-any.whl (23.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for cross_ai_core-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e231765da34676cdb85916ff3225b9244cccbb1aea18c4290fd9d19d73b00e15
MD5 3852c1b8dae4c466bfb2af1c577b56f6
BLAKE2b-256 d8397f21fbde016486aa646a381ddddca4a42f23aff7768f0ac08d1a60999281

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for cross_ai_core-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 62373e5e3f2097829a58fd89d5cb13ace82ee177765ac0a7987f4f874dfa479b
MD5 5939a2037cf282492f7c86314356b9a5
BLAKE2b-256 d21c448b67e5caac7de762e69fc433eb0c84a12c611fef25d3cfda83e8412f4f

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