npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

lunar-sdk

v0.2.0

Published

TypeScript SDK for Lunar LLM Inference API - OpenAI-compatible with intelligent fallbacks

Readme

Lunar SDK

TypeScript SDK for Lunar LLM Inference API - OpenAI-compatible with intelligent fallbacks.

Installation

npm install lunar

Quick Start

import { Lunar } from "lunar";

// Initialize client (uses LUNAR_API_KEY env var)
const client = new Lunar();

// Chat completion
const response = await client.chat.completions.create({
  model: "gpt-4o-mini",
  messages: [{ role: "user", content: "Hello!" }],
});

console.log(response.choices[0].message.content);
console.log(`Cost: $${response.usage?.total_cost_usd}`);

Authentication

Set your API key via environment variable:

export LUNAR_API_KEY="your-api-key"

Or pass it directly:

const client = new Lunar({ apiKey: "your-api-key" });

JWT authentication is also supported:

const client = new Lunar({ jwt: "eyJhbG..." });

Features

Chat Completions

const response = await 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?" },
  ],
});

console.log(response.choices[0].message.content);

Streaming

Stream responses token by token:

const stream = await client.chat.completions.create({
  model: "gpt-4o-mini",
  messages: [{ role: "user", content: "Write a short story" }],
  stream: true,
});

for await (const chunk of stream) {
  if (chunk.choices[0].delta.content) {
    process.stdout.write(chunk.choices[0].delta.content);
  }
}

Text Completions

const response = await client.completions.create({
  model: "gpt-4o-mini",
  prompt: "Once upon a time",
  max_tokens: 100,
});

console.log(response.choices[0].text);

Fallbacks

Lunar automatically falls back to alternative models when the primary model fails with infrastructure errors (5xx, rate limits, timeouts).

// Per-request fallbacks
const response = await client.chat.completions.create({
  model: "gpt-4o-mini",
  messages: [{ role: "user", content: "Hello!" }],
  fallbacks: ["claude-3-haiku", "llama-3.1-8b"],
});

// Global fallbacks via config
const client = new Lunar({
  fallbacks: {
    "gpt-4o-mini": ["claude-3-haiku", "llama-3.1-8b"],
    "gpt-4": ["claude-3-opus"],
  },
});

Fallback behavior:

  • Triggers fallback: 5xx errors, 429 rate limit, connection errors, timeouts
  • Does NOT trigger fallback: 400 bad request, 401 auth error, 403 forbidden (client errors won't be fixed by another model)

Force Provider

const response = await client.chat.completions.create({
  model: "openai/gpt-4o-mini", // Forces OpenAI provider
  messages: [{ role: "user", content: "Hello!" }],
});

List Models and Providers

// List available models
const models = await client.models.list();
for (const model of models) {
  console.log(`${model.id} (owned by ${model.owned_by})`);
}

// List providers for a model
const providers = await client.providers.list("gpt-4o-mini");
for (const provider of providers) {
  console.log(`${provider.id}: ${provider.type} (enabled: ${provider.enabled})`);
}

Cost Tracking

Every response includes detailed cost information:

const response = await client.chat.completions.create({
  model: "gpt-4o-mini",
  messages: [{ role: "user", content: "Hello!" }],
});

console.log(`Input tokens: ${response.usage?.prompt_tokens}`);
console.log(`Output tokens: ${response.usage?.completion_tokens}`);
console.log(`Input cost: $${response.usage?.input_cost_usd}`);
console.log(`Output cost: $${response.usage?.output_cost_usd}`);
console.log(`Total cost: $${response.usage?.total_cost_usd}`);
console.log(`Latency: ${response.usage?.latency_ms}ms`);

Configuration

const client = new Lunar({
  apiKey: "your-api-key",                // Or use LUNAR_API_KEY env var
  baseUrl: "https://api.lunar-sys.com", // Custom API endpoint
  timeout: 60000,                        // Request timeout in ms
  numRetries: 3,                         // Retries for transient errors
  fallbacks: {                           // Global fallback configuration
    "gpt-4o-mini": ["claude-3-haiku"],
  },
});

Error Handling

import {
  Lunar,
  LunarError,
  APIError,
  BadRequestError,
  AuthenticationError,
  RateLimitError,
  ServerError,
} from "lunar";

const client = new Lunar();

try {
  const response = await client.chat.completions.create({
    model: "gpt-4o-mini",
    messages: [{ role: "user", content: "Hello!" }],
  });
} catch (e) {
  if (e instanceof BadRequestError) {
    console.log(`Invalid request: ${e}`);
  } else if (e instanceof AuthenticationError) {
    console.log(`Auth failed: ${e}`);
  } else if (e instanceof RateLimitError) {
    console.log(`Rate limited: ${e}`);
  } else if (e instanceof ServerError) {
    console.log(`Server error: ${e}`);
  } else if (e instanceof APIError) {
    console.log(`API error [${e.statusCode}]: ${e}`);
  } else if (e instanceof LunarError) {
    console.log(`Lunar error: ${e}`);
  }
}

License

MIT