Prompt Optimization
ThinkLang includes built-in prompt optimization utilities for improving output quality over time.
Example Store
The ExampleStore records prompt-result pairs and provides them as few-shot examples for future calls.
typescript
import { ExampleStore, optimizedThink } from "thinklang";
const store = new ExampleStore();
// First call — no examples, learns from result
const result1 = await optimizedThink({
jsonSchema: schema,
prompt: "Classify this email",
exampleStore: store,
});
// Second call — injects first result as a few-shot example
const result2 = await optimizedThink({
jsonSchema: schema,
prompt: "Classify this other email",
exampleStore: store,
});Global Example Store
A globalExampleStore singleton is available. optimizedThink() uses it by default.
typescript
import { globalExampleStore, optimizedThink } from "thinklang";
// Uses globalExampleStore automatically
const result = await optimizedThink({ jsonSchema: schema, prompt: "..." });
// Check stored examples
console.log(globalExampleStore.count(schema));Prompt Variation Search
Use optimize() to try multiple prompt variations against an evaluation dataset:
typescript
import { optimize } from "thinklang";
const result = await optimize({
prompts: [
"Classify the sentiment of this text",
"What is the emotional tone of this text?",
"Rate the positivity of this text",
],
evalSet: [
{ input: "I love this!", expected: { label: "positive" } },
{ input: "Terrible.", expected: { label: "negative" } },
],
scorer: (result, expected) => result.label === expected.label ? 1 : 0,
thinkOptions: { jsonSchema: sentimentSchema },
});
console.log(result.bestPrompt); // The prompt that scored highest
console.log(result.bestScore); // Score from 0 to 1API Reference
ExampleStore
record(schema, prompt, result, score?)— Store a prompt-result pairretrieve(schema, n?)— Get top N examples for a schemacount(schema)— Number of stored examplesclear()— Remove all examples
optimizedThink(options)
Like think() but injects few-shot examples and records results. Extra options:
exampleStore?— Custom store (default:globalExampleStore)maxExamples?— Max examples to inject (default: 3)
optimize(options)
prompts— Array of prompt variations to testevalSet— Array of{ input, expected }pairsscorer— Function(result, expected) => numberreturning 0-1thinkOptions— Base options forthink()(prompt is overridden)