SelfAgency
Describe what you want in plain language, get working methods back.SelfAgency is a mixin module that gives any Ruby class the ability to
generate and install methods at runtime via an LLM.
Key Features
|
|
[!CAUTION] This is an experiment. It may not be fit for any specific purpose. Its micro-prompting. Instead of asking Claude Code, CodeX or Gemini to create an entire application, you can use SelfAgency to generate individual methods. So far the experiments are showing good success with methods that perform math stuff on its input.
Quick Example¶
require "self_agency"
SelfAgency.configure do |config|
config.provider = :ollama
config.model = "qwen3-coder:30b"
config.api_base = "http://localhost:11434/v1"
end
class Calculator
include SelfAgency
end
calc = Calculator.new
calc._("an instance method to add two integers, return the result")
#=> [:add]
calc.add(3, 7)
#=> 10
How It Works¶
Your casual description is first "shaped" into a precise Ruby method specification, then passed through a multi-stage pipeline:
- Shape -- Rewrites your casual description into a precise Ruby method specification
- Generate -- Produces
def...endblock(s) from the shaped spec - Sanitize -- Strips markdown fences and
<think>blocks - Validate -- Checks for empty code, missing
def...end, syntax errors, and dangerous patterns - Retry -- On validation/security failure, feeds the error back to the LLM for self-correction (up to
generation_retriesattempts) - Sandbox Eval -- Evaluates code inside a cached sandbox module that shadows dangerous Kernel methods
Requirements¶
- Ruby >= 3.2.0
- An LLM provider (Ollama by default, or any provider supported by ruby_llm)
Getting Started¶
Head to the Installation guide to add SelfAgency to your project, then follow the Quick Start for a complete walkthrough. For a broader perspective on where SelfAgency fits in your development process, see How to Use SelfAgency.