# Streams, embeddings and the model packages

How a provider package maps streams and embeddings, and what Jadpo's
own OpenAI, Anthropic and Gemini packages send. You need the mapping rules only
to write a package; an app picks a package and connects it
([Connecting a provider](/docs/the-ai-capability.md#connecting-a-provider)).

- **`stream:`** takes `call:`, `input:` and `pieces:`, required, and
  `refused_when:`, `cut_off_when:`, `done_when:` and `usage:`. `call:` is
  an operation that changes nothing, as `generate:`'s, its output type
  the shape of one event's data; its input is mapped as `generate:`'s,
  and a value written there (`stream: true`) asks the provider to
  stream. `pieces: event.<field>...` is where an event's piece of text
  is, `first` a list's first item and a field after a list with no
  `first` every item's text joined, as `answer:` reads them; `cut_off_when:` and `done_when:` are
  `{ path: equals: }` on each event as sent. A stream is whole at `data:
  [DONE]` or an event `done_when:` marks; an `error` event, or one with
  an `error` member, breaks it; an event that marks nothing and has no
  text (a ping, a thinking block's) is passed over. Its events are read
  on the answer as sent, not decoded, since a provider's events differ in
  shape; their type checks the mapping and the stream's recording.
  `usage:` is read and checked but not counted, since a stream is
  charged the whole of the most it could use.
- **`embed:`** takes `call:`, `input:`, `vectors:`, `vector:`, `batch:`
  and `usage:`, required, and `index:`. Its input maps `request.model`,
  `request.texts` (onto a list of text) and `request.dimensions`, `ai: {
  embedding_dimensions: }`, besides settings and values; or, for a
  provider that takes a request for each text, `{ each: request.texts
  as: { } }` onto a list of objects, `item` the text and `request.model`
  and `request.dimensions` as outside it. The texts are required, one way
  or the other; `vectors: response.<field>` is the answer's list
  of objects, `vector: item.<field>` each item's vector (a `Vector` or a
  list of numbers) and `index: item.<field>` its place among the texts.
  `batch:` is the most texts the provider takes at once, from 1: an
  `ai.embed_all` of more is sent as several requests of `batch:` texts,
  one after another in order, within the call's one `timeout:`. Their
  vectors are joined in the texts' order and their usage added up, so
  the call is one step, one recording and one charge. A request that
  fails ends the call with its failure, and nothing after it is sent.
  A text longer than the
  model's context in the package's table is `too_long` before sending. The answer
  is decoded by the package's types, then kept in the built-in driver's
  form, `usage.prompt_tokens` from `usage: { input: }`, so a rerun, a
  record and the budget read it as theirs.
- **`refused_when:`** is a place that holds anything, or `{
  holds: <place>  path: "<member>"  equals: <value> }`, any of them
  saying so, since OpenAI marks a refusal with a field and its content
  filter with a finish reason. `equals:` may list values, any of them
  saying so, as Gemini stops a candidate for its content with one of
  several.
- **`answer:`** reads every item of a list where a field follows the list
  with no `first` (`response.content.text`), their texts joined in order
  and the items with none left out, as Anthropic answers in content
  blocks, a thinking block among them with no text.
- **`schema: { envelope: }`** names the envelope's shape:
  `AiEnvelope.openai` (the default, `{"type": "json_schema",
  "json_schema": {"name", "strict", "schema"}}`),
  `AiEnvelope.anthropic` (`{"type": "json_schema", "schema"}` at
  `output_config.format`) or `AiEnvelope.gemini` (`"responseMimeType":
  "application/json"` and `"responseJsonSchema"`, written beside what
  the mapping puts at `at:`, Gemini's `generationConfig`, which holds
  `maxOutputTokens` too), with the schema cut as
  [Generating, its fake and the local driver](/docs/generating-its-fake-and-the-local-driver.md)
  says, which Anthropic's and Gemini's structured outputs also take;
  Gemini's has no `const`, so a `const` goes as an `enum` of its one
  value. Gemini's `json_only` is `"responseMimeType": "application/json"`
  alone. Anthropic has no JSON-only mode, so `json_only` with its
  envelope, and any envelope with `none`, is `package.provider_invalid`.
  Wherever an envelope is an object and the mapping has already written
  an object at `at:`, the envelope's members go beside the mapping's.
- **`CredentialScheme.header`** sends the credential alone in the header
  `credential_header:` names (Anthropic's `x-api-key`, Gemini's
  `x-goog-api-key`), a new provider
  key required for it and only for it; and **`headers:`**, another new
  key, lists the fixed headers every request carries, each `"Name:
  value"` (Anthropic's `anthropic-version: 2023-06-01`). Neither may name
  a header the driver writes itself (`Authorization`, `Content-Type`,
  `Accept`, `User-Agent`, `Host`, `Content-Length` and AWS's own). The
  surface record lists both.
- **A sandbox's embeddings.** A connection's `development: { sandbox: }`
  may name `embed`, the operation embeddings are logged as, when it is
  the `ai:` connection and its package maps `embed:`.
- **A provider package's types** read its answers leniently (a `T?`
  member left out is `none`, a member the type doesn't declare ignored)
  whether or not a connection names the package, so `jadpo package
  check` decodes its recordings as an app does.
- **`ai: { prices: }`** sets a price for a model by its name in
  `models:`, overriding its package's table: `input:` and
  `output:`, required, and `cached:` (the input's when left out), each a
  `Money` setting giving the price per million tokens, all three in one
  currency (`misconfigured` at the call otherwise). A price for a model
  `models:` doesn't name, or one that isn't money, is
  `ai.settings_invalid`.
- **The OpenAI package**, `OpenAi.v1`, [one of Jadpo's
  own](/docs/where-packages-come-from.md#jadpos-own-packages), installed with `jadpo package
  add @jadpo/open-ai`, is written
  from OpenAI's API reference and model pages: Chat Completions, `POST
  /chat/completions` with `model`, `messages` (the instructions as the
  `system` message), `max_completion_tokens` (`max_tokens` is
  deprecated) and the strict `response_format`; the answer
  `choices.0.message.content`; refused for `message.refusal` or a
  `content_filter` finish, cut off for a `length` finish; usage
  `prompt_tokens`, `completion_tokens` and
  `prompt_tokens_details.cached_tokens`. It streams the same request with
  `"stream": true` (its own input type, so a request that doesn't stream
  sends no `stream`), each event's `choices.0.delta.content` a piece,
  ended by `data: [DONE]`; and embeds through `POST /embeddings` with
  `model`, `input` and `dimensions`, reading `data[i].embedding` by
  `index`, up to 2,048 texts a request. Its key is a bearer credential;
  the connection gives `host:`, `https://api.openai.com/v1`, so a
  compatible gateway or a test's server can stand in. Its failures are
  its operations' (`error.code`), and none is mapped. So a refusal of the
  request is `misconfigured`, and a `429` or `5xx` is `unavailable`, as
  for every package's operation.
- **The Anthropic package**, `Anthropic.v1`, [one of Jadpo's
  own](/docs/where-packages-come-from.md#jadpos-own-packages), installed with `jadpo package
  add @jadpo/anthropic`, is
  written from Anthropic's Messages reference and guides: `POST
  /v1/messages` with `model`, `max_tokens`, `system` (the instructions)
  and `messages`, the schema at `output_config.format` (its structured
  outputs, out of beta; `output_format` is deprecated); the answer every
  `text` block's text; refused for a `refusal` stop reason, cut off for
  `max_tokens`; usage `input_tokens`, `output_tokens` and
  `cache_read_input_tokens`. It streams the same request with `"stream":
  true`, each `content_block_delta`'s `delta.text` a piece, a
  `message_delta`'s `delta.stop_reason` marking a refusal or a cut-off,
  ended by `message_stop`. Its key goes in `x-api-key` and its version,
  `2023-06-01`, in `anthropic-version`; errors are `error.type`. Anthropic
  has no embeddings endpoint, so the package maps no `embed:`, and an
  app embedding through it is `ai.settings_invalid` at the call. Its
  current models think by default, and their thinking counts against
  `max_tokens`, so against `max_output:`; the package leaves thinking as
  each model sets it, since the setting that turns it down differs by
  model.
- **The Gemini package**, `Gemini.v1`, is written from Google's Gemini
  API reference and guides, through `generateContent`, which the
  reference marks legacy but supported, rather than the Interactions
  API it leads with, since only `generateContent` documents why a
  candidate stopped and why a prompt was blocked, which a refusal and a
  cut-off are read from, and it keeps nothing unless asked. `POST
  /v1beta/models/{model}:generateContent` takes `systemInstruction` (the
  instructions, one text part), `contents` (each message a turn of one
  text part, `role` `user` or `model`, the model's turns spelled
  `model`), `generationConfig` with `maxOutputTokens` and the schema's
  `responseMimeType` and `responseJsonSchema`, and `store: false`; the
  answer is every text part of the first candidate, a function call's
  part having none; refused when `promptFeedback.blockReason` holds
  anything or the candidate's `finishReason` is `SAFETY`, `RECITATION`,
  `BLOCKLIST`, `PROHIBITED_CONTENT` or `SPII`, cut off at `MAX_TOKENS`;
  usage `promptTokenCount`, `candidatesTokenCount` and
  `thoughtsTokenCount` added up as output, since thinking is billed as
  output, and `cachedContentTokenCount`. It streams the same request
  through `streamGenerateContent?alt=sse`, each event a whole response
  with a piece of the answer, refused and cut off as `generate`, whole at
  `STOP`; and embeds through `batchEmbedContents`, a request for each
  text naming `models/<model>` with `embedContentConfig.outputDimensionality`,
  reading `embeddings[i].values` in the texts' order, up to 100 texts a
  request (the reference gives no limit; 100 is `ai.embed_all`'s own).
  Its key goes in `x-goog-api-key`; the connection gives `host:`,
  `https://generativelanguage.googleapis.com`. Errors are read at
  `error.status`, and none is mapped. Its models think by default; the
  package leaves thinking as each model sets it and sends no tools,
  since tool calling is left to packages
  ([Questions](https://github.com/jadpo-dev/jadpo/blob/main/docs/language/notes/21-ai.md#questions), 1).
- **The tables** come from the providers' own pages, and are refreshed
  whenever those change. They hold context windows of 1,050,000
  tokens for `gpt-6-astra`, `gpt-6.1-sol` and `gpt-6-luna`, 8,192 tokens
  a text for `text-embedding-3-small` and `-large`, and 1M (taken as
  1,000,000) for `claude-fable-5-1`, `claude-opus-5-5`,
  `claude-sonnet-5-5` and `claude-haiku-5-5`; standard prices per
  million input, cached and output tokens in US dollars, from 2026-10-20:
  OpenAI's 10.00, 1.00 and 50.00 (Astra), 2.00, 0.10 and 10.00 (Sol), and
  0.10, 0.01 and 0.50 (Luna), its embedding models' 0.02 and 0.13 input
  (no output; cached repeating input, which OpenAI doesn't publish);
  Anthropic's 10.00, 0.25 and 50.00 (Fable 5.1), 4.00, 0.20 and 20.00
  (Opus 5.5), 2.00, 0.10 and 10.00 (Sonnet 5.5), and 0.10, 0.01 and 0.50
  (Haiku 5.5, for prompts up to 100,000 tokens). Gemini's hold
  1,048,576 for `gemini-4-hydrogen` and `gemini-3.8-flash`, 1M (taken as
  1,000,000, as only the overview gives it) for `gemini-4-carbon`,
  `gemini-4-neon` and `gemini-4-argon`, and 8,192 and 2,048 tokens a text
  for `gemini-embedding-2` and `gemini-embedding-001`; from 2026-12-01,
  1.50, 0.075 and 7.50 (Hydrogen), 4.00, 0.20 and 20.00 (Carbon and
  Argon), 3.00, 0.15 and 15.00 (Neon), 0.75, 0.075 and 3.75 (3.8 Flash,
  1.50, 0.15 and 7.50 from 2027-01-01), and 0.20 and 0.15 input for its
  embedding models. Long-prompt prices
  (OpenAI's past 272,000 input tokens, Haiku 5.5's past 100,000),
  batch, fast, flex, priority and regional prices, cache writes and
  Gemini's cache storage aren't held; an app
  that needs them sets `ai: { prices: }`.
- **Recordings.** Each package keeps a recording of each operation,
  `recordings/<resource>.<operation>.json`, made by `jadpo package
  record` from the answer each provider's documentation shows, sent by a
  server in the test, never a live call: a stream's is one event, a
  piece of text. A test records them again from the same answers and
  compares them with the files, and `jadpo test` decodes them.
- **The documentation used:**
  - OpenAI: <https://developers.openai.com/api/reference/resources/chat/subresources/completions/methods/create>,
    <https://developers.openai.com/api/reference/resources/chat/subresources/completions/streaming-events>,
    <https://developers.openai.com/api/reference/resources/embeddings/methods/create>,
    <https://developers.openai.com/api/docs/guides/structured-outputs>,
    <https://developers.openai.com/api/docs/guides/error-codes>,
    <https://developers.openai.com/api/docs/models>, each model's page
    under <https://developers.openai.com/api/docs/models/> (`gpt-6-astra`,
    `gpt-6.1-sol`, `gpt-6-luna`, `text-embedding-3-small`,
    `text-embedding-3-large`), and
    <https://developers.openai.com/api/docs/pricing>.
  - Anthropic: <https://platform.claude.com/docs/en/api/messages/create>,
    <https://platform.claude.com/docs/en/build-with-claude/streaming>,
    <https://platform.claude.com/docs/en/build-with-claude/structured-outputs>,
    <https://platform.claude.com/docs/en/build-with-claude/thinking>,
    <https://platform.claude.com/docs/en/api/errors>,
    <https://platform.claude.com/docs/en/api/versioning>,
    <https://platform.claude.com/docs/en/about-claude/models/overview>,
    <https://platform.claude.com/docs/en/build-with-claude/context-windows>
    and <https://platform.claude.com/docs/en/about-claude/pricing>.
  - Gemini, read on 2026-12-01 (each `ai.google.dev` page now redirects
    to the same path under `aistudio.google.com`):
    <https://ai.google.dev/api>,
    <https://ai.google.dev/api/generate-content> and its methods
    `generate-content` and `stream-generate-content`,
    <https://ai.google.dev/api/embeddings> and its method
    `batch-embed-contents`,
    <https://ai.google.dev/gemini-api/docs/text-generation>,
    <https://ai.google.dev/gemini-api/docs/structured-output>,
    <https://ai.google.dev/gemini-api/docs/embeddings>,
    <https://ai.google.dev/gemini-api/docs/migrate-to-interactions>,
    <https://ai.google.dev/gemini-api/docs/models> with the pages of
    `gemini-4-hydrogen` and `gemini-3.8-flash`, and
    <https://ai.google.dev/gemini-api/docs/pricing>. Its reference shows
    no sample answer, so its recordings are made from answers holding the
    members the reference lists.
