Documentation
¶
Overview ¶
Package tokipe assembles LLM token-optimization stages into a pipeline.
Quickstart:
kit := tokipe.New(client,
config.WithPreprocess(myRules...),
config.WithRAG(embedder, store, 5),
config.WithDefaultCompression(),
config.WithCacheAlignment(),
config.WithRouter(router.NewHeuristicRouter(cheap, strong)),
)
resp, err := kit.Run(ctx, &pipeline.Request{Query: "…"})
Every optimization is opt-in and every one fails open: if compression, retrieval, a tool executor, or a cache backend breaks — including by panicking — the turn still reaches the model, and Run returns no error for it.
Run returns an error in exactly three cases, none of them an optimization failure:
- the model call itself failed;
- ctx was cancelled or its deadline passed;
- a caller-supplied Stage added with config.WithStage returned an error, or a stage wrote a malformed short-circuit value into Metadata. Both surface as *pipeline.StageError naming the stage responsible.
One case is neither returned nor contained: a Stage you supplied via config.WithStage that *panics* propagates the panic to your caller. Run does not recover it. Your stage is your code running in your pipeline; recovering it would hide your bug rather than tolerate a third party's. If you want that panic contained, recover inside your own Process method.
Everything tokipe itself calls into — preprocess rules, tool executors, compressors, embedders, vector stores, routers, and every Name method — is wrapped, and a panic there is treated exactly like the equivalent error.
New enforces the stage ordering the spec requires (retrieval before compression, both before cache alignment, routing last). Callers who need a different order must compose pipeline.New directly and own that decision.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func New ¶
New builds a Pipeline from client and the enabled options. client is the default model endpoint; a router configured via config.WithRouter may override it per request.
Passing no options yields a pipeline that simply forwards to client, which is a valid — and useful — baseline to measure the optimizations against.
func NewFromConfig ¶
NewFromConfig is New for callers that already hold a resolved Config.
Types ¶
type Chunk ¶
Re-exported so a caller can build a request and read a response without importing the subpackages directly.
type Message ¶
Re-exported so a caller can build a request and read a response without importing the subpackages directly.
type Request ¶
Re-exported so a caller can build a request and read a response without importing the subpackages directly.
type Response ¶
Re-exported so a caller can build a request and read a response without importing the subpackages directly.
type Stage ¶
Re-exported so a caller can build a request and read a response without importing the subpackages directly.
Directories
¶
| Path | Synopsis |
|---|---|
|
Command benchmarks measures billed input tokens for the same synthetic workload with and without tokipe, against the acceptance criterion in spec §3.2: ≥30% reduction in billed input tokens.
|
Command benchmarks measures billed input tokens for the same synthetic workload with and without tokipe, against the acceptance criterion in spec §3.2: ≥30% reduction in billed input tokens. |
|
Package budget expresses how much context a turn is allowed to spend, keyed by what kind of turn it is.
|
Package budget expresses how much context a turn is allowed to spend, keyed by what kind of turn it is. |
|
Package cache implements the CacheAlignStage (docs/spec.md §2.4.6): it reorders a Request's messages so that the parts a provider can cache come first, then emits the cache breakpoints marking that cacheable prefix.
|
Package cache implements the CacheAlignStage (docs/spec.md §2.4.6): it reorders a Request's messages so that the parts a provider can cache come first, then emits the cache breakpoints marking that cacheable prefix. |
|
Package compress implements the CompressStage (docs/spec.md §2.4.4): it walks Request.RetrievedChunks and rewrites each chunk's content with the first registered Compressor that claims it.
|
Package compress implements the CompressStage (docs/spec.md §2.4.4): it walks Request.RetrievedChunks and rewrites each chunk's content with the first registered Compressor that claims it. |
|
Package config is the functional-options surface for assembling a pipeline.
|
Package config is the functional-options surface for assembling a pipeline. |
|
examples
|
|
|
cli-provider
command
Command cli-provider runs tokipe against a command-line coding agent instead of an HTTP API — no API key, no separate billing, just whatever CLI your subscription already authenticates.
|
Command cli-provider runs tokipe against a command-line coding agent instead of an HTTP API — no API key, no separate billing, just whatever CLI your subscription already authenticates. |
|
coding-agent
command
Command coding-agent is the most complete example: a long-running, multi-turn agent loop with heavy tool use and a growing context, wiring every stage together — preprocess, toolcache, rag, compress, lazyload, cache alignment, routing, and budget.
|
Command coding-agent is the most complete example: a long-running, multi-turn agent loop with heavy tool use and a growing context, wiring every stage together — preprocess, toolcache, rag, compress, lazyload, cache alignment, routing, and budget. |
|
local-routing
command
Command local-routing demonstrates cost-aware model selection: a mixed workload split across a cheap "local" model and an expensive "cloud" model purely by HeuristicRouter, with no per-request routing code below.
|
Command local-routing demonstrates cost-aware model selection: a mixed workload split across a cheap "local" model and an expensive "cloud" model purely by HeuristicRouter, with no per-request routing code below. |
|
observability
command
Command observability renders a terminal dashboard from tokipe's own counters, histograms and degradation events.
|
Command observability renders a terminal dashboard from tokipe's own counters, histograms and degradation events. |
|
rag-chatbot
command
Command rag-chatbot demonstrates the retrieval path end to end against mocks only — no API key, no database, no network.
|
Command rag-chatbot demonstrates the retrieval path end to end against mocks only — no API key, no database, no network. |
|
streaming
command
Command streaming demonstrates Pipeline.RunStream — the same stages, with an incremental result.
|
Command streaming demonstrates Pipeline.RunStream — the same stages, with an incremental result. |
|
Package history trims a conversation to fit a token budget.
|
Package history trims a conversation to fit a token budget. |
|
internal
|
|
|
safe
Package safe contains the recover boundary tokipe puts around caller-supplied code.
|
Package safe contains the recover boundary tokipe puts around caller-supplied code. |
|
Package lazyload defines the reference/resolver contract that lets an agent carry cheap handles in its prompt and pull the expensive bytes only when the model actually asks for them (docs/spec.md §2.4.5).
|
Package lazyload defines the reference/resolver contract that lets an agent carry cheap handles in its prompt and pull the expensive bytes only when the model actually asks for them (docs/spec.md §2.4.5). |
|
Package metrics defines the minimal, provider-agnostic counter interface stages use to report what they did.
|
Package metrics defines the minimal, provider-agnostic counter interface stages use to report what they did. |
|
otel
module
|
|
|
Package pipeline defines the core contract of tokipe: the Request that flows through every optimization stage, the Response returned to the caller, the Stage extension point, and the Pipeline that runs them in order.
|
Package pipeline defines the core contract of tokipe: the Request that flows through every optimization stage, the Response returned to the caller, the Stage extension point, and the Pipeline that runs them in order. |
|
Package preprocess resolves deterministic requests without invoking an LLM.
|
Package preprocess resolves deterministic requests without invoking an LLM. |
|
examples
Package examples holds reference preprocess.Rule implementations.
|
Package examples holds reference preprocess.Rule implementations. |
|
Package providers holds LLM provider adapters.
|
Package providers holds LLM provider adapters. |
|
anthropic
Package anthropic implements pipeline.ModelClient against the Anthropic Messages API using nothing but net/http and encoding/json.
|
Package anthropic implements pipeline.ModelClient against the Anthropic Messages API using nothing but net/http and encoding/json. |
|
cli
Package cli adapts any command-line coding agent into a pipeline.ModelClient by running it as a subprocess.
|
Package cli adapts any command-line coding agent into a pipeline.ModelClient by running it as a subprocess. |
|
mock
Package mock provides a ModelClient test double.
|
Package mock provides a ModelClient test double. |
|
openai
Package openai implements pipeline.ModelClient against the OpenAI chat-completions API — and, because that shape has become a de facto standard, against everything that speaks it: Ollama, vLLM, llama.cpp, Groq, Together, OpenRouter, LM Studio and Azure OpenAI.
|
Package openai implements pipeline.ModelClient against the OpenAI chat-completions API — and, because that shape has become a de facto standard, against everything that speaks it: Ollama, vLLM, llama.cpp, Groq, Together, OpenRouter, LM Studio and Azure OpenAI. |
|
Package rag implements the retrieval-augmented-generation stage: it embeds the current query, searches a vector store, and attaches the results to Request.RetrievedChunks.
|
Package rag implements the retrieval-augmented-generation stage: it embeds the current query, searches a vector store, and attaches the results to Request.RetrievedChunks. |
|
Package router selects which ModelClient serves the final LLM call, based on a cheap, deterministic estimate of how hard the request is.
|
Package router selects which ModelClient serves the final LLM call, based on a cheap, deterministic estimate of how hard the request is. |
|
Package stores defines the retrieval interfaces tokipe depends on.
|
Package stores defines the retrieval interfaces tokipe depends on. |
|
mock
Package mock provides in-memory, network-free test doubles for the stores.Embedder and stores.VectorStore interfaces.
|
Package mock provides in-memory, network-free test doubles for the stores.Embedder and stores.VectorStore interfaces. |
|
pgvector
module
|
|
|
Package toolcache caches tool-call results keyed by a deterministic hash of the tool name and its arguments.
|
Package toolcache caches tool-call results keyed by a deterministic hash of the tool name and its arguments. |
|
redis
module
|