Powered by AppSignal & Oban Pro

Jidoka: Tools And Context

livebook/02_tools_and_context.livemd

Jidoka: Tools And Context

Run in Livebook

Add deterministic application code to an agent and give that agent a small context schema.

Setup

Chat cells use a Livebook secret named ANTHROPIC_API_KEY. The direct tool cell runs without a provider key.

Mix.install(
  [
    {:jidoka, git: "https://github.com/mikehostetler/jidoka.git", ref: "924a486f3c1b7e7a943cb3d5ceee0de65f158467"},
    {:kino, "~> 0.19.0"}
  ],
  config: [
    jidoka: [
      model_aliases: %{
        fast: "anthropic:claude-haiku-4-5"
      }
    ]
  ]
)
Jidoka.Kino.setup()

Define A Tool

Tools are deterministic application work exposed to the model as callable actions. Jidoka tools use Zoi schemas and compile to Jido actions underneath.

defmodule LivebookDemo.Tools.AddNumbers do
  use Jidoka.Tool,
    name: "add_numbers",
    description: "Adds two integers.",
    schema:
      Zoi.object(%{
        a: Zoi.integer(),
        b: Zoi.integer()
      }),
    output_schema:
      Zoi.object(%{
        sum: Zoi.integer()
      })

  @impl true
  def run(%{a: a, b: b}, _context) do
    {:ok, %{sum: a + b}}
  end
end

Run the tool directly:

LivebookDemo.Tools.AddNumbers.run(%{a: 20, b: 22}, %{tenant: "demo"})

Attach The Tool To An Agent

defmodule LivebookDemo.MathAgent do
  use Jidoka.Agent

  agent do
    id :livebook_math_agent

    schema Zoi.object(%{
      tenant: Zoi.string() |> Zoi.default("demo"),
      session: Zoi.string() |> Zoi.optional()
    })
  end

  defaults do
    model :fast
    instructions "Use tools when they help. Keep the final answer short."
  end

  capabilities do
    tool LivebookDemo.Tools.AddNumbers
  end
end

Inspect the agent’s context defaults and published tool names:

%{
  context_defaults: LivebookDemo.MathAgent.context(),
  tool_names: LivebookDemo.MathAgent.tool_names()
}

Run A Tool Call

{:ok, pid} =
  Jidoka.Kino.start_or_reuse("livebook-math-agent", fn ->
    LivebookDemo.MathAgent.start_link(id: "livebook-math-agent")
  end)

Jidoka.Kino.chat("Tool call", fn ->
  LivebookDemo.MathAgent.chat(
    pid,
    "Use the add_numbers tool to add 20 and 22. Reply with only the sum.",
    context: %{session: "livebook"}
  )
end)

Next

Continue with Workflows And Imports.