agentic examples

The three-line agent

The three-line agent. Run me with no API key at all:

Developer Experience exit 0

source on github

bundle exec ruby examples/haiku_agent.rb

a real captured run

Autumn at first light
an old pond holds the whole sky
ruby leaves drift down

source

# frozen_string_literal: true

# The three-line agent. Run me with no API key at all:
#
#   bundle exec ruby examples/haiku_agent.rb
#
# An agent, a capability, a result - each expressed the way Ruby wants
# to express it: a block, a lambda, a hash. Nothing here talks to a
# network; capabilities are just callables, so the whole plan-and-execute
# idea is graspable in one screen.

require class="s">"bundler/setup"
require class="s">"agentic"

# 1. An agent in three lines
poet = Agentic:class="y">:Agent.build do |a|
  a.name = class="s">"Basho"
  a.role = class="s">"Haiku poet"
end

# 2. A capability is a specification plus any callable
haiku = Agentic:class="y">:CapabilitySpecification.new(
  name: class="s">"haiku",
  description: class="s">"Compose a haiku about a topic",
  version: class="s">"1.0.0",
  inputs: {topic: {type: class="s">"string", required: true}},
  outputs: {poem: {type: class="s">"string"}}
)

brush = Agentic:class="y">:CapabilityProvider.new(
  capability: haiku,
  implementation: ->(inputs) {
    {poem: [
      class="s">"#{inputs[class="y">:topic].capitalize} at first light",
      class="s">"an old pond holds the whole sky",
      class="s">"ruby leaves drift down"
    ].join(class="s">"\n")}
  }
)

Agentic.register_capability(haiku, brush)
poet.add_capability(class="s">"haiku")

# 3. Ask the poet for a poem
puts poet.execute_capability(class="s">"haiku", {topic: class="s">"autumn"})[class="y">:poem]

# And when you do have an API key, the same agent, the same message,
# a real LLM - only the provider changes:
#
#   Agentic.configure { |c| c.access_token = ENV["OPENAI_ACCESS_TOKEN"] }
#   plan = Agentic::TaskPlanner.new("Write a haiku about autumn").plan
#   puts plan.to_s