Programs
Declare a task
sig = Imp.signature("question -> answer")
sig =
Imp.signature(
"ticket -> team: enum[billing,security], urgent: boolean",
"Route the support ticket."
)
Run it
program = Imp.predict(sig, lm: lm)
{:ok, prediction} = Imp.call(program, %{ticket: "..."})
Imp.get(prediction, :team)
See what was sent
prediction.metadata.trace.messages
Reason first
Imp.chain_of_thought(sig, lm: lm)
Typed JSON output, with one retry
Imp.predict(sig, lm: lm, adapter: Imp.Adapter.JSON, config: [json_retries: 1])
Improve
Add labeled examples (no model calls)
Imp.optimize!(program, Imp.Optimizer.LabeledFewShot.new(k: 8), trainset)
Bootstrap examples the program gets right
Imp.optimize!(program, Imp.Optimizer.BootstrapFewShot.new(metric), trainset)
Search over candidates
optimizer = Imp.Optimizer.BootstrapFewShotWithRandomSearch.new(metric)
Imp.optimize!(program, optimizer, trainset, valset)
Rewrite instructions from feedback
optimizer = Imp.Optimizer.GEPA.new(metric, reflection_lm: strong_lm, max_metric_calls: 400)
Imp.optimize!(program, optimizer, trainset, valset)
weather =
Imp.tool(:weather, "Current weather for a city.", fn %{"city" => city} ->
Weather.lookup(city)
end,
schema: %{
"type" => "object",
"properties" => %{"city" => %{"type" => "string"}},
"required" => ["city"]
}
)
An agent loop
agent = Imp.react("question -> answer", [weather], lm: lm, max_iters: 5)
{:ok, prediction} = Imp.call(agent, %{question: "Is it raining in Lisbon?"})
# Each step's tool calls and their results.
prediction.metadata.history.messages
server = %{"name" => "docs", "type" => "http", "url" => "https://example.com/mcp"}
{:ok, imported} = Imp.MCP.connect([server], trusted_servers: [server])
agent = Imp.react("question -> answer", imported.tools, lm: lm)
imported.cleanup.()