package chatoyant

Build not yet checked
  1. Overview
  2. Docs
OCaml-first LLM SDK with Melange-generated JavaScript

Install

dune-project
 Dependency

Authors

Maintainers

Sources

chatoyant-v0.18.0.tbz
sha256=3c003dd1e14cfa4656de04ab97118dac151be5233df885eab5b09744584a745b
sha512=510b59defbbf182697947937b33e3a3546ab5796a0962f1126d5fa8ec813ca891a655376853115676a40c72b1780b381603d7feca0a78f1f638e7535fec51686

Description

Chatoyant is an OCaml-first SDK for LLM providers. It exposes an Eio native API, typed JSON Schema and tool definitions, raw provider clients for OpenAI/Anthropic/xAI/Meta/OpenRouter/local inference, and a Melange-generated JavaScript package.

README

Chatoyant

npm version CI OCaml TypeScript License: MIT

Chatoyant Logo

OCaml-first SDK for LLM providers with an Eio native runtime and a zero-runtime-dependency JavaScript package generated by Melange. Chatoyant provides typed provider clients, structured outputs, tool calling, streaming, token/cost accounting, and root-only npm exports.

chatoyant /shuh-TOY-uhnt/ - having a changeable lustre.

Install

npm install chatoyant            # JavaScript/TypeScript: Node 18+, no runtime dependencies
opam install chatoyant eio_main  # OCaml 5.3+

Set the API key for the provider you call (OPENAI_API_KEY, ANTHROPIC_API_KEY, XAI_API_KEY, META_API_KEY or OPENROUTER_API_KEY), then make a first call:

import { genText } from "chatoyant";

console.log(await genText("Say hello in three words.", { model: "fast" }));

In OCaml, add (libraries chatoyant eio_main) to your dune stanza, plus (preprocess (pps chatoyant.ppx)) for module%tool and derived codecs. Full examples: OCaml Quick Start and JavaScript Quick Start.

Highlights

  • Native OCaml API built around Eio, result-returning calls, .mli contracts, and typed tool definitions.
  • Melange-generated npm package with one root import path, ESM and CommonJS entrypoints, colocated .d.ts and .d.cts declarations, and no runtime npm dependencies.
  • Unified Chat session API, one-shot text/data/stream shortcuts, tool calling, streaming accumulation, JSON roundtrip, and token/cost accounting.
  • Raw provider clients for OpenAI, Anthropic, xAI, Meta (Muse Spark), OpenRouter, and local OpenAI-compatible servers.
  • Standalone Draft 2020-12 JSON Schema parser/validator with OpenAI strict projection and typed OCaml codec generation.
  • Production-derived JS usage tests, OCaml native tests, TypeScript declaration checks, package metadata checks, and the official JSON Schema suite against the bundled npm package.

Guides

Full API docs live at anonyfox.github.io/chatoyant. Start with one of these:

Package Size Next To Other JS LLM Libraries

Measured on 2026-11-10 with Node v24.22.1 and npm 11.21.1. Each case uses fresh production installs of registry latest versions. Install sizes count node_modules file bytes (1 MB = 1024 KB); bundles use a one-request script, esbuild 0.25 (minified ESM for Node 20), and gzip level 9.

SDK

Versions

Installed packages

node_modules

Minified bundle

Gzip

chatoyant

chatoyant@0.17.2

1

0.6 MB

218.0 KB

54.9 KB

openai

openai@7.55.0

1

22.9 MB

385.0 KB

84.2 KB

@anthropic-ai/sdk

@anthropic-ai/sdk@1.7.0

8

17.6 MB

413.5 KB

97.3 KB

ai + @ai-sdk/openai

ai@7.0.166, @ai-sdk/openai@4.0.111

12

22.5 MB

916.2 KB

208.2 KB

langchain + @langchain/openai

langchain@1.6.3, @langchain/core@1.3.6, @langchain/openai@1.8.2

25

78.1 MB

1520.6 KB

356.1 KB

Chatoyant has the smallest install and bundles in this sample; openai ties its installed package count. The openai and Anthropic cases each cover one provider; chatoyant includes all six provider adapters plus JSON Schema validation and token/cost tables. One root import still pulls all adapters into the bundle.

See Smallest JavaScript LLM SDK: measured sizes for the method, source scripts, measured JSON and a typechecked example. Rerun with node scripts/measure-sdk-size.mjs.

Provider Routing

Provider

Env var

Detection

OpenAI

OPENAI_API_KEY

gpt-*, o1-*, o3-*, o4-*, chatgpt-*

Anthropic

ANTHROPIC_API_KEY

claude-*

xAI

XAI_API_KEY

grok-*

Meta

META_API_KEY (or MODEL_API_KEY)

muse-*

OpenRouter

OPENROUTER_API_KEY

Slash notation such as openai/gpt-4o

Local

LOCAL_BASE_URL

Explicit provider: "local" or local fallback

Model presets are available for quick intent-based calls: fast, cheap, balanced, best, and reasoning.

Preset

OpenAI

Anthropic

xAI

Meta

fast / cheap

gpt-6-luna

claude-haiku-5-5

grok-4.20-0309-non-reasoning

muse-spark-1.3

balanced

gpt-6.2-sol

claude-sonnet-5-5

grok-4.3

muse-spark-1.3

best / reasoning

gpt-6.2-astra

claude-fable-5-2

grok-4.7

muse-spark-1.3

Also recognized: gpt-6.1-terra (text/image input, 1,050,000 context tokens, 128,000 max output tokens), gpt-6-cyber, gpt-6-sol, gpt-5.6-cyber, gpt-5.5-cyber, gpt-5.5-cyber-preview, gpt-5.3-codex, chat-latest, gpt-5-search-api, claude-mythos-5-2, gpt-6-astra, gpt-6.1-sol, claude-fable-5-1, claude-mythos-5-1, claude-mythos-5, claude-opus-5-5, claude-opus-5, grok-4.6, grok-build-0.1, muse-spark-1.3-contributor, muse-wonder-1.0, muse-ember-1.0, grok-imagine-image-2.0, grok-imagine-image-quality, grok-imagine-video-1.5, and grok-imagine-video-1.5-lite.

GPT-6 Cyber has 1,050,000 context tokens and costs 20/100/2 per million input/output/cached input tokens. Claude Fable/Mythos 5.2 have 1,000,000 context tokens and cost $8/40/0.20, with 5m/1h cache writes at $10/16. Muse Ember 1.0 has 1,048,576 context tokens and costs 0.10/0.50/0.004; input above 200K tokens (including cached tokens) uses $0.30/1.50/$0.012 rates that the accounting table does not model. These explicit IDs do not change the preset mappings above.

GPT-6.2 Astra and Sol each support 128,000 output tokens in a 1,050,000-token context window. The pricing tables use Standard rates. GPT-6.2 Astra for Law uses promotional pricing through at least December 31, 2026.

OpenAI prices use the short-context tier (up to 272K input tokens); higher long-context rates are not modelled. GPT-5.6 Sol uses OpenAI's promotional price through 2026-11-30. Haiku 5.5 prompts above 100K tokens and Grok Build prompts at or above 200K tokens have higher rates not modelled here.

Retired on first-party APIs (ids still resolve for pricing/detection): claude-opus-4-1, claude-opus-4, claude-sonnet-4, claude-3-5-haiku/claude-3-; xAI no longer lists grok-4-1-fast-, grok-code-fast-1, grok-3*, grok-4-0709.

OCaml Quick Start

open Chatoyant

let () =
  Eio_main.run @@ fun env ->
  let ai = Chatoyant.openai ~model:"gpt-6-luna" env in
  match Chatoyant.gen_text ai "Say hello in three words." with
  | Ok text -> print_endline text
  | Error err -> prerr_endline (Chatoyant.Error.provider err)

Typed tools are ordinary modules. Comments become schema descriptions, option means optional, and the generated tool value plugs into a chat.

module%tool Calculate = struct
  type operation =
    | Add
    | Divide

  type request = {
    operation : operation; (** Operation to apply. *)
    values : float list [@min_items 1]; (** Numbers to combine in order. *)
  }

  type answer = { result : float }

  (** Combine numbers with a typed arithmetic operation. *)
  let run : request -> (answer, string) result =
   fun { operation; values } ->
    match operation, values with
    | _, [] -> Error "at least one value is required"
    | Add, values -> Ok { result = List.fold_left ( +. ) 0. values }
    | Divide, first :: rest ->
        List.fold_left
          (fun acc value -> Result.bind acc (fun n ->
             if value = 0. then Error "division by zero" else Ok (n /. value)))
          (Ok first) rest
        |> Result.map (fun result -> { result })
end

let () =
  Eio_main.run @@ fun env ->
  let ai =
    Chatoyant.openai ~model:"gpt-6-luna" ~tools:[ Calculate.tool ] env
  in
  ignore (Chatoyant.gen_text ai "Divide 83 by 3.")

For the full native guide, see OCAML.md.

JavaScript Quick Start

import { Chat, Schema, createTool, genData, genStream, genText } from "chatoyant";

const text = await genText("What is 2+2?", {
  model: "fast",
  system: "Return short answers.",
});

class Person extends Schema {
  name = Schema.String({ description: "Person name" });
  age = Schema.Integer({ minimum: 0 });
}

const person = await genData("Extract: Alice is 30 years old", Person);

for await (const chunk of genStream("Write a haiku about typed APIs.")) {
  process.stdout.write(chunk);
}

const lookup = createTool({
  name: "lookup",
  description: "Lookup data",
  parameters: { q: Schema.String({ minLength: 1 }) },
  async execute({ args }) {
    return { found: args.q };
  },
});

const chat = new Chat({ model: "gpt-4o" });
chat.system("Use tools when useful.").user("Find needle").addTool(lookup);
console.log(await chat.generate());

All JavaScript imports come from the package root:

import { Chat, OpenAI, OpenRouter, Tokens, JsonSchema, genText } from "chatoyant";

Former subpath imports from the TypeScript package intentionally move to this root surface. See JAVASCRIPT.md.

Build And Test

From the repository root:

make build   # OCaml + Melange build, then the esbuild npm bundle
make test    # native tests, JS parity tests, tsc, and the JSON Schema suite
make check   # the full release gate
make help    # list all targets

make check builds and tests the native OCaml package first, emits Melange ESM, bundles the npm package with esbuild, runs Node's native tests against dist/index.js, checks dist/index.d.ts with tsc, runs the pinned official JSON Schema suite through the bundled package, lints opam metadata, checks local documentation links, verifies package metadata, and dry-packs the npm artifact.

See CONTRIBUTING.md for setup, the project layout, and the release process, and CHANGELOG.md for release history.

Support

If this package helps your project, consider sponsoring its maintenance: GitHub Sponsors.

Anonyfox | MIT License

Dependencies (16)

  1. x509 >= "1.0.0"
  2. uri
  3. tls-eio >= "1.0.0"
  4. tls >= "1.0.0"
  5. ppxlib build
  6. mirage-crypto-rng >= "1.0.0"
  7. http >= "6.0.0"
  8. eio >= "1.0"
  9. domain-name
  10. digestif
  11. cohttp-eio >= "6.0.0"
  12. base64
  13. ca-certs >= "1.0.0"
  14. melange >= "6.0.0"
  15. dune >= "3.23"
  16. ocaml >= "5.3"

Dev Dependencies (2)

  1. odoc with-doc
  2. eio_main with-test

Optional Dependencies

None

Used by

None

Conflicts

None

Build status

Results of building chatoyant with each compiler on check.ci.ocaml.org, run of against opam-repository 8d8d144. All results for this package.

Each result links to its build log. To keep crawlers from overloading the site, build logs require a GitHub sign-in (see ocurrent/opam-health-check). On your first log, GitHub asks you to “Authorize opam-health-check”, the OCurrent app that runs these checks; it sees only your public profile, not your repositories. The sign-in lasts 30 days.

This version is not in the latest run; the versions below are.

Version OCaml 4.11 OCaml 4.14 OCaml 5.2 OCaml 5.4 OCaml 5.5
0.17.4 n/a n/a n/a good good
  • good Package successfully built
  • partial One of the dependencies failed to build
  • bad Package failed to build
  • n/a Package is not available in this environment
  • internal Internal failure