Language Tour
A quick tour of ThinkLang's features. Each section links to its detailed guide.
Types
Define structured types that the AI must conform to:
type Sentiment {
label: string
score: float
tags: string[]
}Primitives: string, int, float, bool, null. Supports arrays (T[]), optionals (T?), unions (A | B), and the Confident<T> wrapper.
think
The core primitive. Calls the LLM with a prompt and returns a typed result:
let summary = think<Summary>("Summarize this article")
with context: articleinfer
Lightweight inference on an existing value:
let lang = infer<string>("Bonjour le monde", "Detect the language")reason
Multi-step reasoning with explicit steps:
let analysis = reason<Analysis> {
goal: "Evaluate the investment portfolio"
steps:
1. "Assess current allocation"
2. "Analyze market conditions"
3. "Formulate recommendation"
with context: { portfolio, market }
}Context
Scope data for AI calls with with context and exclude sensitive data with without context:
let result = think<Report>("Analyze this data")
with context: { userData, metrics }
without context: sensitiveDataConfidence
Track AI confidence with Confident<T> and require explicit handling of uncertain values:
let uncertain result = think<Confident<Category>>("Classify this item")
with context: item
let safe = result.expect(0.8)Guards
Constrain AI output with validation rules:
let text = think<string>("Write a summary")
guard { length: 50..300 }
on_fail: retry(3)Match
Pattern match on values, including AI results:
let response = match sentiment {
{ confidence: >= 0.9 } => "High confidence"
{ confidence: >= 0.5 } => "Moderate confidence"
_ => "Manual review needed"
}Pipeline
Chain operations with the |> operator:
let result = rawText
|> think<Keywords>("Extract keywords") with context: rawText
|> think<Report>("Write a report from these keywords")Error Handling
Catch specific AI error types:
try {
let result = think<Summary>("Summarize") with context: text
} catch SchemaViolation (e) {
print "Schema error"
} catch ConfidenceTooLow (e) {
print "Low confidence"
}Testing
Built-in test blocks with semantic assertions:
test "sentiment is positive" {
let s = think<Sentiment>("Analyze sentiment") with context: review
assert s.label == "positive"
assert.semantic(s, "indicates a happy customer")
}Modules
Split code across files with import. All top-level types and functions are importable:
import { Sentiment, analyzeSentiment } from "./types.tl"
let result = analyzeSentiment("Great product!")
print resultNo export keyword needed. Paths are relative to the importing file. Circular imports are detected.
Full guide: Syntax Reference -- Imports
Cost Tracking
Monitor AI usage and costs:
thinklang run app.tl --show-cost
thinklang cost-report