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

@nfinitmonkeys/cortex-sdk

v1.2.0

Published

Official TypeScript SDK for the Cortex LLM Gateway API

Downloads

296

Readme

@nfinitmonkeys/cortex-sdk

Official TypeScript SDK for the Cortex LLM Gateway API.

Installation

npm install @nfinitmonkeys/cortex-sdk

Requires Node.js 18+ (uses native fetch).

Quick Start

import { CortexClient } from '@nfinitmonkeys/cortex-sdk';

const cortex = new CortexClient({ apiKey: 'sk-cortex-...' });

// Chat completion
const response = await cortex.chat.completions.create({
  model: 'default',
  messages: [{ role: 'user', content: 'Hello!' }],
});
console.log(response.choices[0].message.content);

Configuration

const cortex = new CortexClient({
  apiKey: 'sk-cortex-...',               // Required
  llmBaseUrl: 'https://custom-api.com/v1', // Default: https://cortexapi.nfinitmonkeys.com/v1
  adminBaseUrl: 'https://custom-admin.com', // Default: https://admin.nfinitmonkeys.com
  timeout: 30000,                          // Request timeout (ms), default: 30000
  streamTimeout: 300000,                   // Streaming timeout (ms), default: 300000
  maxRetries: 3,                           // Retry count, default: 3
  defaultHeaders: { 'X-Custom': 'value' }, // Extra headers on every request
});

LLM Gateway

Chat Completions

// Non-streaming
const chat = await cortex.chat.completions.create({
  model: 'gpt-4',
  messages: [
    { role: 'system', content: 'You are a helpful assistant.' },
    { role: 'user', content: 'What is TypeScript?' },
  ],
  temperature: 0.7,
  max_tokens: 500,
});

// Streaming
const stream = await cortex.chat.completions.create({
  model: 'gpt-4',
  messages: [{ role: 'user', content: 'Tell me a story' }],
  stream: true,
});

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

// Collect streamed text
const text = await stream.toText();

Text Completions

const completion = await cortex.completions.create({
  model: 'gpt-3.5-turbo-instruct',
  prompt: 'Once upon a time',
  max_tokens: 100,
});

Embeddings

const embeddings = await cortex.embeddings.create({
  model: 'text-embedding-ada-002',
  input: 'The quick brown fox',
});

Models

const models = await cortex.models.list();

Admin / Platform API

API Keys

const keys = await cortex.keys.list();
const newKey = await cortex.keys.create({ name: 'Production Key' });
await cortex.keys.delete('key-id');

Teams

const teams = await cortex.teams.list();
const team = await cortex.teams.create({ name: 'Engineering' });
const details = await cortex.teams.get('team-id');
await cortex.teams.delete('team-id');

// Members
await cortex.teams.members.add('team-id', { userId: 'user-id', role: 'member' });
await cortex.teams.members.update('team-id', 'member-id', { role: 'admin' });
await cortex.teams.members.remove('team-id', 'member-id');

Usage & Performance

const usage = await cortex.usage.get({ startDate: '2024-01-01', granularity: 'daily' });
const limits = await cortex.usage.limits();
const perf = await cortex.performance.get();

Conversations

const convos = await cortex.conversations.list({ limit: 10 });
const convo = await cortex.conversations.create({ title: 'New Chat' });
const details = await cortex.conversations.get('conv-id');
await cortex.conversations.update('conv-id', { title: 'Renamed' });
await cortex.conversations.delete('conv-id');

// Stream messages (SSE)
const msgStream = await cortex.conversations.messages('conv-id');
for await (const chunk of msgStream) {
  console.log(chunk);
}

Iris (Document Extraction)

const result = await cortex.iris.extract({
  document: 'John Doe, age 30, lives in NYC.',
  schema: { name: 'string', age: 'number', city: 'string' },
});

const jobs = await cortex.iris.jobs({ limit: 10 });
const schemas = await cortex.iris.schemas();

Plugins, PDF, Web Search

const plugins = await cortex.plugins.list();

const pdf = await cortex.pdf.generate({ content: '# Report\nContent here.' });

const results = await cortex.webSearch.search({ query: 'TypeScript best practices' });

Error Handling

All errors extend CortexError:

import {
  CortexError,
  AuthenticationError,
  RateLimitError,
  ValidationError,
  TimeoutError,
  ConnectionError,
  NotFoundError,
  ServerError,
} from '@nfinitmonkeys/cortex-sdk';

try {
  await cortex.chat.completions.create({ ... });
} catch (error) {
  if (error instanceof RateLimitError) {
    console.log(`Rate limited. Retry after ${error.retryAfter}s`);
  } else if (error instanceof AuthenticationError) {
    console.log('Invalid API key');
  } else if (error instanceof ValidationError) {
    console.log(`Validation failed on field: ${error.field}`);
  } else if (error instanceof TimeoutError) {
    console.log('Request timed out');
  }
}

API keys are never exposed in error messages or stack traces.

Cancellation

Use AbortController to cancel requests:

const controller = new AbortController();

setTimeout(() => controller.abort(), 5000);

const response = await cortex.chat.completions.create(
  { model: 'gpt-4', messages: [{ role: 'user', content: 'Hello' }] },
  { signal: controller.signal },
);

Retry Behavior

The SDK automatically retries on status codes 429, 500, 502, 503, 504 with exponential backoff and jitter. It respects Retry-After headers. Configure with maxRetries (default: 3).

Development

npm install
npm run build
npm test

License

MIT