# Embed Turn chess positions and chess text into vectors in one shared space, so a position lands near the words that describe it. Mix both in one call. `POST /v1/embed` turns each item you send into a vector of ChessDrive Embed, the Embedding model. Positions and texts share one space, so you can find positions from words, and positions like a position, in your own collection. It's at its best on positions and words that share a tactical idea. [Search](/docs/search) finds real games beyond your own: the moves played from a position, and a player's games. ## 1. Embed positions and texts `model` is required and takes a versioned Model ID, `chessdrive-embed-1`; there is no alias (see [one Model version](#compare-vectors-only-within-one-model-version) below). `input` is a list of 1 to 256 items, positions and texts mixed in any order, each typed: - A position: `"type": "position"` with a `fen`, or with a `pgn` and a `ply`, as on every API. The two give different vectors, so [send positions the same way](#send-positions-the-same-way). - A text: `"type": "text"` with a `text` of at most 77 tokens, about 300 characters. Text is never truncated: a longer one is refused. ```bash curl curl https://api.chessdrive.io/v1/embed \ -H "Authorization: Bearer $CHESSDRIVE_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "chessdrive-embed-1", "input": [ { "type": "position", "fen": "4kb1r/p2n1ppp/4q3/4p1B1/4P3/1Q6/PPP2PPP/2KR4 w k - 0 16" }, { "type": "position", "pgn": "1. e4 c5 2. Nf3 d6 3. d4 cxd4 4. Nxd4 Nf6 5. Nc3 a6", "ply": 10 }, { "type": "text", "text": "a queen sacrifice that ends in mate" } ] }' ``` ```python Python import os import requests r = requests.post( "https://api.chessdrive.io/v1/embed", headers={"Authorization": f"Bearer {os.environ['CHESSDRIVE_API_KEY']}"}, json={ "model": "chessdrive-embed-1", "input": [ {"type": "position", "fen": "4kb1r/p2n1ppp/4q3/4p1B1/4P3/1Q6/PPP2PPP/2KR4 w k - 0 16"}, {"type": "position", "pgn": "1. e4 c5 2. Nf3 d6 3. d4 cxd4 4. Nxd4 Nf6 5. Nc3 a6", "ply": 10}, {"type": "text", "text": "a queen sacrifice that ends in mate"}, ], }, ) r.raise_for_status() for item in r.json()["data"]: print(item["index"], len(item["embedding"])) ``` ```js JavaScript const res = await fetch("https://api.chessdrive.io/v1/embed", { method: "POST", headers: { Authorization: `Bearer ${process.env.CHESSDRIVE_API_KEY}`, "Content-Type": "application/json", }, body: JSON.stringify({ model: "chessdrive-embed-1", input: [ { type: "position", fen: "4kb1r/p2n1ppp/4q3/4p1B1/4P3/1Q6/PPP2PPP/2KR4 w k - 0 16" }, { type: "position", pgn: "1. e4 c5 2. Nf3 d6 3. d4 cxd4 4. Nxd4 Nf6 5. Nc3 a6", ply: 10 }, { type: "text", text: "a queen sacrifice that ends in mate" }, ], }), }); if (!res.ok) throw new Error(await res.text()); for (const item of (await res.json()).data) console.log(item.index, item.embedding.length); ``` ## 2. Read the vectors `data` holds one vector per item, in the order you sent them, with `index` naming the item. Each `embedding` has `dimensions` numbers, 512 for `chessdrive-embed-1`, and is L2-normalised: its length is 1. You can [ask for shorter or smaller vectors](#get-shorter-or-smaller-vectors) too. `usage` counts the items by kind, `positions` and `texts`, as each kind has its own price on [Pricing](/pricing). The vectors below are cut to four numbers to fit the page. ```json Response { "id": "req_01M4G0B8R2YV6N3QK7TD9XWE5H", "model": "chessdrive-embed-1", "dimensions": 512, "data": [ { "index": 0, "embedding": [0.0213, -0.0472, 0.0118, 0.0391] }, { "index": 1, "embedding": [-0.0087, 0.0335, 0.0502, -0.0164] }, { "index": 2, "embedding": [0.0198, -0.0441, 0.0087, 0.0427] } ], "usage": { "positions": 2, "texts": 1 }, "cost_usd": "0.000120" } ``` ## 3. Compare vectors Because every vector has length 1, the cosine similarity of two vectors is their dot product: 1 for the same item, higher for closer ones. Rank your own positions against a text, or against another position, by it. ```python Python import os import requests r = requests.post( "https://api.chessdrive.io/v1/embed", headers={"Authorization": f"Bearer {os.environ['CHESSDRIVE_API_KEY']}"}, json={ "model": "chessdrive-embed-1", "input": [ {"type": "text", "text": "a queen sacrifice that ends in mate"}, {"type": "position", "fen": "4kb1r/p2n1ppp/4q3/4p1B1/4P3/1Q6/PPP2PPP/2KR4 w k - 0 16"}, {"type": "position", "fen": "rnbqkb1r/1p2pppp/p2p1n2/8/3NP3/2N5/PPP2PPP/R1BQKB1R w KQkq - 0 6"}, ], }, ) r.raise_for_status() query, *positions = [item["embedding"] for item in r.json()["data"]] for i, vector in enumerate(positions, start=1): similarity = sum(a * b for a, b in zip(query, vector)) print(f"position {i}: {similarity:.3f}") ``` ```js JavaScript const res = await fetch("https://api.chessdrive.io/v1/embed", { method: "POST", headers: { Authorization: `Bearer ${process.env.CHESSDRIVE_API_KEY}`, "Content-Type": "application/json", }, body: JSON.stringify({ model: "chessdrive-embed-1", input: [ { type: "text", text: "a queen sacrifice that ends in mate" }, { type: "position", fen: "4kb1r/p2n1ppp/4q3/4p1B1/4P3/1Q6/PPP2PPP/2KR4 w k - 0 16" }, { type: "position", fen: "rnbqkb1r/1p2pppp/p2p1n2/8/3NP3/2N5/PPP2PPP/R1BQKB1R w KQkq - 0 6" }, ], }), }); if (!res.ok) throw new Error(await res.text()); const [query, ...positions] = (await res.json()).data.map((item) => item.embedding); const dot = (a, b) => a.reduce((sum, x, i) => sum + x * b[i], 0); positions.forEach((vector, i) => console.log(`position ${i + 1}: ${dot(query, vector).toFixed(3)}`)); ``` ## 4. Get smaller responses with base64 By default each `embedding` is a list of numbers. With `"encoding_format": "base64"` it is a base64 string of the vector's float32 values, little-endian, which is about a third of the size. Decode it back to 512 floats. ```python Python import base64 import os import struct import requests r = requests.post( "https://api.chessdrive.io/v1/embed", headers={"Authorization": f"Bearer {os.environ['CHESSDRIVE_API_KEY']}"}, json={ "model": "chessdrive-embed-1", "encoding_format": "base64", "input": [{"type": "text", "text": "a queen sacrifice that ends in mate"}], }, ) r.raise_for_status() raw = base64.b64decode(r.json()["data"][0]["embedding"]) vector = struct.unpack(f"<{len(raw) // 4}f", raw) print(len(vector), vector[:4]) ``` ```js JavaScript const res = await fetch("https://api.chessdrive.io/v1/embed", { method: "POST", headers: { Authorization: `Bearer ${process.env.CHESSDRIVE_API_KEY}`, "Content-Type": "application/json", }, body: JSON.stringify({ model: "chessdrive-embed-1", encoding_format: "base64", input: [{ type: "text", text: "a queen sacrifice that ends in mate" }], }), }); if (!res.ok) throw new Error(await res.text()); const bytes = Uint8Array.from(atob((await res.json()).data[0].embedding), (c) => c.charCodeAt(0)); const view = new DataView(bytes.buffer); const vector = Array.from({ length: bytes.length / 4 }, (_, i) => view.getFloat32(i * 4, true)); console.log(vector.length, vector.slice(0, 4)); ``` ## 5. Get shorter or smaller vectors `chessdrive-embed-1` puts its most useful dimensions first, so the start of a vector is a good vector on its own. Both fields are optional, and the price is the same. - `dimensions` is 64, 128, 256 or 512: each vector is its first numbers, at length 1 again. - `precision` is `float32`, `int8` or `binary`. `int8` gives each number as an integer from -127 to 127. `binary` gives each number's sign, 1 for zero or above, packed 8 to a byte with the first dimension in the high bit: `dimensions` / 8 integers from 0 to 255. With `"encoding_format": "base64"`, an int8 vector is one signed byte per number and a binary one is its packed bytes. | `dimensions` | float32 | int8 | binary | |---|---|---|---| | 512 | 2,048 bytes | 512 bytes | 64 bytes | | 256 | 1,024 bytes | 256 bytes | 32 bytes | | 128 | 512 bytes | 128 bytes | 16 bytes | | 64 | 256 bytes | 64 bytes | 8 bytes | In our tests int8 finds about the same positions as float32 at the same length, and each shorter length loses a little. Binary loses more, and suits a first rough pass. Compare int8 vectors by cosine similarity, which needs their lengths now, and binary ones by how many bits differ. Compare only vectors of the same `dimensions` and `precision`. ```python Python import math import os import requests r = requests.post( "https://api.chessdrive.io/v1/embed", headers={"Authorization": f"Bearer {os.environ['CHESSDRIVE_API_KEY']}"}, json={ "model": "chessdrive-embed-1", "dimensions": 128, "precision": "int8", "input": [ {"type": "text", "text": "a queen sacrifice that ends in mate"}, {"type": "position", "fen": "4kb1r/p2n1ppp/4q3/4p1B1/4P3/1Q6/PPP2PPP/2KR4 w k - 0 16"}, ], }, ) r.raise_for_status() query, position = [item["embedding"] for item in r.json()["data"]] dot = sum(a * b for a, b in zip(query, position)) similarity = dot / (math.hypot(*query) * math.hypot(*position)) print(len(query), f"{similarity:.3f}") ``` ```js JavaScript const res = await fetch("https://api.chessdrive.io/v1/embed", { method: "POST", headers: { Authorization: `Bearer ${process.env.CHESSDRIVE_API_KEY}`, "Content-Type": "application/json", }, body: JSON.stringify({ model: "chessdrive-embed-1", dimensions: 128, precision: "int8", input: [ { type: "text", text: "a queen sacrifice that ends in mate" }, { type: "position", fen: "4kb1r/p2n1ppp/4q3/4p1B1/4P3/1Q6/PPP2PPP/2KR4 w k - 0 16" }, ], }), }); if (!res.ok) throw new Error(await res.text()); const [query, position] = (await res.json()).data.map((item) => item.embedding); const dot = (a, b) => a.reduce((sum, x, i) => sum + x * b[i], 0); const similarity = dot(query, position) / Math.sqrt(dot(query, query) * dot(position, position)); console.log(query.length, similarity.toFixed(3)); ``` ## Send positions the same way A position with its game, a `pgn` and a `ply`, carries the boards before it. A bare `fen` is the board alone. The same board gets a different vector each way, so choose one and send every position you compare that way. ## Compare vectors only within one Model version Vectors from different versions of the Embedding model live in different spaces, so comparing them gives meaningless numbers. That is why `model` takes only a versioned ID such as `chessdrive-embed-1` and never an alias that could move under you. - Store the `model` from each response next to the vectors you keep. - When you move to a new version, embed your whole collection again with it, then switch your queries over. Never mix versions in one index. ## Use it from an agent An agent never sees a vector: it writes code that works on a file on your machine. - **Make the file:** [`chessdrive embed --out`](/docs/cli) or the local [`embed` tool](/docs/mcp) writes the vectors to a `.npy` file, with a line of what each row is in a `.jsonl` file beside it. The file is always float32; `--dimensions` (`dimensions` on the tool) makes its vectors shorter. - **Work on it:** the `chessdrive-vectors` Skill, in the [Plugin](/skills), teaches the agent to compare and group that file, and never to read vectors into the chat. The `chessdrive-game-search` Skill builds on it to find an idea across your PGN files. This needs an agent that runs code, such as Claude Code, Codex or Cursor. Vectors, Patterns and Game search also run in Claude's chat app on a paid plan, once the player has allowed our domain: see [the Claude tab](/cookbook/add-to-your-ai). The hosted Connector has no `embed` tool. ## Limits and errors One bad item refuses the whole call, with a 400 whose `param` names the item, such as `input[3].fen`. Nothing is billed. | Code | When | |---|---| | `too_many_inputs` | More than 256 items. Split them over several calls. | | `text_too_long` | A text past 77 tokens, about 300 characters. `param` names it, and nothing is billed. | | `missing_field`, `invalid_field` | An item without its `type` or its content, a `type` other than `position` or `text`, or an empty text. Also a `dimensions` other than 64, 128, 256 or 512, or a `precision` other than `float32`, `int8` or `binary`. | | `invalid_fen`, `invalid_pgn`, `invalid_ply` | A position that can't be read. | | `unknown_model` | Any `model` that isn't an Embedding model version, Knight and Queen included. | Every field is in the [API reference](/docs/api#embed).