Embedding Chunk Planner — Go source
Plan document chunking for RAG — chunk counts with overlap math, vector counts, and embedding costs per model.
This is the Go implementation — the same logic the interactive tool runs, in a shareable, citable form.
// Package embeddingchunkplanner — plan how a document splits into overlapping
// embedding chunks and price the embedding call (pure chunking math for RAG).
//
// Language: Go (standard library only).
// CosmoDev polyglot showcase port of the `embedding-chunk-planner` tool,
// ported from src/lib/embeddingPlanner.ts (the canonical TypeScript
// implementation).
//
// The live Go twin (cli/embedding-planner) imports the shared aimodels
// package and sits outside this page's resolver path, so this display
// snippet inlines the SSOT price table from src/lib/ai/embeddings.ts —
// prices never live in the planner.
//
// Display source — part of CosmoDev's polyglot tool pages.
package embeddingchunkplanner
import (
"math"
"slices"
)
// Default chunking knobs, in tokens. Mirror DEFAULT_CHUNK_OPTIONS (512/64).
const (
DefaultChunkSize = 512
DefaultOverlap = 64
)
// ChunkOptions configures PlanChunks / PlanEmbedding. Fields are *int so a
// nil field means "use the default" while an explicit 0 (or negative) is
// used verbatim — exactly like the TS lib's Partial<ChunkOptions>.
type ChunkOptions struct {
ChunkSize *int // nil → DefaultChunkSize (512)
Overlap *int // nil → DefaultOverlap (64)
}
// ChunkPlan mirrors the ChunkPlan interface of the TS lib.
type ChunkPlan struct {
Chunks int
TotalTokensWithOverlap int
OverheadTokens int
}
// EmbeddingModel mirrors the EmbeddingModel interface of src/lib/ai/embeddings.ts.
type EmbeddingModel struct {
ID string
Vendor string
Dims []int // offered embedding dimensions, ascending
InputPerM float64 // USD per 1M input tokens
}
// EmbeddingModels mirrors EMBEDDING_MODELS (the SSOT table; same order).
var EmbeddingModels = []EmbeddingModel{
{ID: "text-embedding-3-small", Vendor: "OpenAI", Dims: []int{512, 1536}, InputPerM: 0.02},
{ID: "text-embedding-3-large", Vendor: "OpenAI", Dims: []int{256, 1024, 3072}, InputPerM: 0.13},
{ID: "embed-english-v3.0", Vendor: "Cohere", Dims: []int{512, 1024, 1536}, InputPerM: 0.1},
{ID: "voyage-3-lite", Vendor: "Voyage AI", Dims: []int{512, 1024}, InputPerM: 0.02},
}
// GetEmbeddingModel finds a model by id; nil when unknown, like the TS lib.
func GetEmbeddingModel(id string) *EmbeddingModel {
for i := range EmbeddingModels {
if EmbeddingModels[i].ID == id {
return &EmbeddingModels[i]
}
}
return nil
}
// EmbeddingPlan mirrors the EmbeddingPlan interface: a ChunkPlan plus the
// model priced, one vector per chunk, and the USD cost.
type EmbeddingPlan struct {
ChunkPlan
Model *EmbeddingModel
Vectors int // one vector per chunk
Cost float64 // USD: TotalTokensWithOverlap / 1e6 * Model.InputPerM
}
// PlanChunks plans how totalTokens split into overlapping chunks. Go twin of
// planChunks() in src/lib/embeddingPlanner.ts:
//
// - chunkSize <= 0 or totalTokens <= 0 → zero plan (nothing to embed).
// - negative overlap → 0, then clamped to floor(chunkSize / 2) so chunks advance.
// - chunks = max(1, ceil((totalTokens-overlap) / (chunkSize-overlap)))
// — a tiny document still yields one chunk.
func PlanChunks(totalTokens int, opts *ChunkOptions) ChunkPlan {
chunkSize := DefaultChunkSize
overlapRaw := DefaultOverlap
if opts != nil {
if opts.ChunkSize != nil {
chunkSize = *opts.ChunkSize
}
if opts.Overlap != nil {
overlapRaw = *opts.Overlap
}
}
if chunkSize <= 0 || totalTokens <= 0 {
return ChunkPlan{}
}
overlap := min(max(overlapRaw, 0), chunkSize/2)
chunks := max(1, int(math.Ceil(float64(totalTokens-overlap)/float64(chunkSize-overlap))))
total := totalTokens + (chunks-1)*overlap
return ChunkPlan{Chunks: chunks, TotalTokensWithOverlap: total, OverheadTokens: total - totalTokens}
}
// PlanEmbedding chunks a document AND prices its embedding for modelID at
// dims dimensions. Unknown model, or dims the model does not offer → nil,
// mirroring the TS lib returning undefined.
func PlanEmbedding(totalTokens int, modelID string, dims int, opts *ChunkOptions) *EmbeddingPlan {
model := GetEmbeddingModel(modelID)
if model == nil || !slices.Contains(model.Dims, dims) {
return nil
}
plan := PlanChunks(totalTokens, opts)
return &EmbeddingPlan{
ChunkPlan: plan,
Model: model,
Vectors: plan.Chunks,
Cost: (float64(plan.TotalTokensWithOverlap) / 1e6) * model.InputPerM,
}
}
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →