Skip to main content

Quickstart

This guide will help you make your first API call with Lunar SDK.

1. Install the SDK

pip install lunar

2. Set Your API Key

Set the LUNAR_API_KEY environment variable:
export LUNAR_API_KEY="your-api-key"
Or pass it directly to the client:
from lunar import Lunar

client = Lunar(api_key="your-api-key")

3. Make Your First Request

Chat Completion

from lunar import Lunar

client = Lunar()

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "What is the capital of France?"}
    ]
)

print(response.choices[0].message.content)
# Output: The capital of France is Paris.

Text Completion

response = client.completions.create(
    model="gpt-4o-mini",
    prompt="The capital of France is",
    max_tokens=10
)

print(response.choices[0].text)
# Output: Paris, which is also...

4. Check Cost and Usage

Every response includes detailed usage information:
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Hello!"}]
)

# Token counts
print(f"Prompt tokens: {response.usage.prompt_tokens}")
print(f"Completion tokens: {response.usage.completion_tokens}")
print(f"Total tokens: {response.usage.total_tokens}")

# Cost breakdown
print(f"Input cost: ${response.usage.input_cost_usd}")
print(f"Output cost: ${response.usage.output_cost_usd}")
print(f"Total cost: ${response.usage.total_cost_usd}")

# Performance
print(f"Latency: {response.usage.latency_ms}ms")

5. Use Fallbacks

Add fallback models to automatically retry if the primary model fails:
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Hello!"}],
    fallbacks=["claude-3-haiku", "llama-3.1-8b"]
)

6. Async Usage

For async applications, use AsyncLunar:
from lunar import AsyncLunar

async def main():
    async with AsyncLunar() as client:
        response = await client.chat.completions.create(
            model="gpt-4o-mini",
            messages=[{"role": "user", "content": "Hello!"}]
        )
        print(response.choices[0].message.content)

Next Steps