package chatoyant
Build not yet checkedInstall
dune-project
Dependency
Authors
Maintainers
Sources
sha256=3c003dd1e14cfa4656de04ab97118dac151be5233df885eab5b09744584a745b
sha512=510b59defbbf182697947937b33e3a3546ab5796a0962f1126d5fa8ec813ca891a655376853115676a40c72b1780b381603d7feca0a78f1f638e7535fec51686
doc/README.html
Chatoyant
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,
.mlicontracts, and typed tool definitions. - Melange-generated npm package with one root import path, ESM and CommonJS entrypoints, colocated
.d.tsand.d.ctsdeclarations, and no runtime npm dependencies. - Unified
Chatsession 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:
- Call GPT-6, Claude 5.5 and Grok 4.7 from OCaml (Eio): install, a first call per provider, tools, structured output and per-request cost.
- Structured LLM output with JSON Schema in OCaml and TypeScript: typed results that are validated before your code sees them.
- Smallest JavaScript LLM SDK: measured sizes: install and bundle sizes next to the openai, Anthropic, AI SDK and LangChain packages.
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 |
|
|
Anthropic |
|
|
xAI |
|
|
Meta |
|
|
OpenRouter |
| Slash notation such as |
Local |
| Explicit |
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 targetsmake 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.