Skip to main content

Installation

Requirements

  • Python 3.10+ (recommended) — also supports 3.9, 3.11, 3.12
  • pip package manager

Install

pip install lunar

Configuration

Environment Variables

VariableDescriptionRequired
LUNAR_API_KEYAPI key for authenticationYes
Set your API key:
export LUNAR_API_KEY="your-api-key"

Client Initialization

Basic usage with environment variable:
from lunar import Lunar

client = Lunar()  # Uses LUNAR_API_KEY from environment
With explicit API key:
client = Lunar(api_key="your-api-key")
Full configuration:
client = Lunar(
    api_key="your-api-key",
    timeout=60.0,                 # Request timeout in seconds
    num_retries=2,                # Retry attempts for failed requests
    max_connections=100,          # Max concurrent connections
    fallbacks={                   # Global fallback configuration
        "gpt-4o-mini": ["claude-3-haiku", "llama-3.1-8b"],
        "gpt-4": ["claude-3-opus"]
    }
)

Configuration Parameters

ParameterTypeDefaultDescription
api_keystrNoneAPI key for authentication
base_urlstrNoneCustom API URL
timeoutfloat60.0Request timeout in seconds
num_retriesint2Number of retry attempts
max_connectionsint100Maximum concurrent connections
fallbacksdictNoneModel fallback configuration

Using Context Manager

The client supports context managers for automatic resource cleanup: Synchronous:
from lunar import Lunar

with Lunar(api_key="your-api-key") as client:
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": "Hello!"}]
    )
# Resources automatically cleaned up
Asynchronous:
from lunar import AsyncLunar

async with AsyncLunar(api_key="your-api-key") as client:
    response = await client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": "Hello!"}]
    )
# Resources automatically cleaned up

Verify Installation

Run this script to verify your installation:
from lunar import Lunar

try:
    client = Lunar()
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": "Say 'Hello from Lunar!'"}],
        max_tokens=20
    )
    print("Success:", response.choices[0].message.content)
    print(f"Cost: ${response.usage.total_cost_usd}")
except Exception as e:
    print(f"Error: {e}")

Next Steps