Skip to main content

LlamaIndex LLM Integration for Cloudflare AI Gateway

This package provides integration between LlamaIndex and Cloudflare AI Gateway, allowing you to use multiple AI models from different providers with automatic fallback.

Features

  • Multiple Providers: Use OpenAI, Anthropic, Mistral, Groq, and more
  • Automatic Fallback: If one provider fails, automatically tries the next
  • Streaming Support: Both chat and completion streaming
  • Async Support: Full async/await support
  • Caching: Built-in request caching with Cloudflare AI Gateway

Installation

pip install llama-index-llms-cloudflare-ai-gateway

Quick Start

from llama_index.llms.cloudflare_ai_gateway import CloudflareAIGateway
from llama_index.llms.openai import OpenAI
from llama_index.llms.anthropic import Anthropic
from llama_index.core.llms import ChatMessage

# Create LLM instances
openai_llm = OpenAI(model="gpt-4o-mini", api_key="your-openai-key")
anthropic_llm = Anthropic(
    model="claude-3-5-sonnet-20241022", api_key="your-anthropic-key"
)

# Create Cloudflare AI Gateway LLM
llm = CloudflareAIGateway(
    llms=[openai_llm, anthropic_llm],  # Try OpenAI first, then Anthropic
    account_id="your-cloudflare-account-id",
    gateway="your-ai-gateway-name",
    api_key="your-cloudflare-api-key",
)

# Use the LLM
messages = [ChatMessage(role="user", content="What is 2+2?")]
response = llm.chat(messages)
print(response.message.content)

Supported Providers

  • OpenAI
  • Anthropic
  • Mistral AI
  • Groq
  • DeepSeek
  • Perplexity
  • Replicate
  • Grok
  • Azure OpenAI

Testing

# Set environment variables
export OPENAI_API_KEY="your-key"
export ANTHROPIC_API_KEY="your-key"
export CLOUDFLARE_ACCOUNT_ID="your-id"
export CLOUDFLARE_API_KEY="your-key"
export CLOUDFLARE_GATEWAY="your-gateway"

# Run tests
uv run pytest tests/

Download files

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

Source Distribution

Built Distribution

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

File details

Details for the file llama_index_llms_cloudflare_ai_gateway-0.3.0.tar.gz.

File metadata

  • Download URL: llama_index_llms_cloudflare_ai_gateway-0.3.0.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.7 {"installer":{"name":"uv","version":"0.12.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for llama_index_llms_cloudflare_ai_gateway-0.3.0.tar.gz
Algorithm Hash digest
SHA256 26942396ac63bdce74d4fd22d4494803d9e9a0c5ce354a995d2fab114ba2deb3
MD5 9e973b316b935179ba23d35ba913f870
BLAKE2b-256 fec08a549ff6233d4ba9880a4657422e50fc716eaefd338bd302f2f32b3ed25f

See more details on using hashes here.

File details

Details for the file llama_index_llms_cloudflare_ai_gateway-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: llama_index_llms_cloudflare_ai_gateway-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.7 {"installer":{"name":"uv","version":"0.12.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for llama_index_llms_cloudflare_ai_gateway-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a5d8eb03c853f860bcdd746b9f62ed6c837e65f638f12b3014d53732b7cba898
MD5 10d4572ed36377c466f4123e4b538428
BLAKE2b-256 dd48334abc52f25e0a53cd38244d89922a90033d2f787b81238a97294d054ef8

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 files

0.2.1

2 files

0.2.0

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page