How to Integrate ADHD into a Custom Agent Using the TypeScript Library API
You can integrate ADHD into a custom TypeScript agent by importing the run function from the adhd package, configuring a RunOptions object with your problem statement and model selections, and awaiting the Promise<RunResult> that contains the full reasoning tree.
The ADHD repository provides a TypeScript library that implements a tree-of-thought engine for generative agents. The core public API is exported from src/index.ts and is designed to be model-agnostic, meaning you supply LLM names through RunOptions and the library handles inference via the internal callLLM helper in src/llm.ts. By importing just the run function and relevant types, you can embed a complete reframe-diverge-score-cluster-deepen cycle into any custom agent with full compile-time safety.
Core Public API Exports
The entry point at src/index.ts exposes a minimal, purpose-built surface:
run– Executes the full reasoning cycle defined insrc/engine.tsand returns aRunResult.renderText– Converts aRunResultinto a human-readable report; implemented insrc/render.ts.FRAMES/selectFrames– Built-in cognitive frames and a selection helper defined insrc/frames.ts.- Types (
Idea,RunOptions,RunResult, …) – All data structures live insrc/types.ts.
Because the API is intentionally small, you only need run and the corresponding types to integrate ADHD into your own codebase.
How the ADHD Engine Works
The reasoning cycle implemented in src/engine.ts proceeds through four logical phases driven by system prompt constants such as DIVERGE_SYSTEM, SCORE_SYSTEM, and DEEPEN_SYSTEM:
- Reframe – The
reframeProblemfunction strips incidental anchors from the input problem statement. - Diverge – The
divergeBranchfunction fires parallel cognitive frames (selected viaselectFrames) and generates raw ideas per frame. - Score and Cluster – The
scoreIdeascritic evaluates each idea, thenclusterIdeasgroups related concepts. - Deepen – The
deepenIdeafunction expands the top-K ideas into concrete sketches and child ideas.
All LLM interactions are handled by callLLM in src/llm.ts, which forwards requests to the Claude Agent SDK. You control behavior entirely through the RunOptions interface without managing prompts manually.
Integration Steps
Follow these steps to integrate ADHD into a custom TypeScript agent:
- Add the library to your project with
npm install adhdor a local path dependency. - Construct a
RunOptionsobject that includes yourproblemstring, optionalcontext, and tuning parameters such asframesPerRun,ideasPerFrame,topK,model, andcriticModel. - Call
run(options)– The function returns aPromise<RunResult>containing the complete reasoning graph. - Consume the result – Use
renderText(result)for CLI output, or processresult.shortlist,result.deepened, andresult.clustersprogrammatically in your agent pipeline.
TypeScript Integration Examples
Example 1: Simple One-Shot Invocation
This minimal example shows how to call the engine and inspect the shortlist and deepened ideas:
import { run, type RunOptions } from "adhd";
async function demo() {
const opts: RunOptions = {
problem: "How can we automatically tag incoming support tickets?",
context: "/* relevant TypeScript utilities */",
framesPerRun: 4,
ideasPerFrame: 5,
topK: 2,
model: "claude-3-sonnet-20240229",
criticModel: "claude-3-opus-20240229",
};
const result = await run(opts);
console.log("Shortlist:", result.shortlist.map(i => i.text));
console.log("Deepened ideas:", result.deepened);
}
demo();
Key points:
- Only
runand theRunOptionstype are imported from the package entry point. - The
RunResultobject contains the full reasoning graph, including theshortlistanddeepenedarrays.
Example 2: Custom Agent Loop with Continuous Feedback
This pattern feeds the engine output back into the next iteration, using the generated provocation to evolve the agent's focus:
import { run, type RunOptions, type RunResult } from "adhd";
async function iterativeAgent(initialProblem: string) {
let problem = initialProblem;
for (let iteration = 0; iteration < 3; ++iteration) {
const opts: RunOptions = {
problem,
framesPerRun: 6,
ideasPerFrame: 8,
topK: 3,
model: "claude-3-sonnet-20240229",
};
const result: RunResult = await run(opts);
const bestSketch = result.deepened[0].sketch;
console.log(`Iteration ${iteration + 1} – focus sketch:\n${bestSketch}`);
problem = result.provocation;
}
}
iterativeAgent("Create a zero‑downtime deployment pipeline");
Key points:
- The loop reuses the
problemor the generatedprovocationto evolve focus across iterations. - The
deepenedarray provides concrete sketches that can be handed off to downstream tools such as code generators.
Key Source Files and Architecture
Understanding the following files will help you debug and extend your integration:
src/index.ts– Public entry point that re-exports the main API.src/engine.ts– Core engine containingrun,reframeProblem,divergeBranch,scoreIdeas,clusterIdeas, anddeepenIdea.src/types.ts– Type definitions forIdea,RunOptions,RunResult, and other data structures.src/frames.ts– Built-in cognitive frames and theselectFrameshelper.src/render.ts– Human-readable rendering logic forRunResult.src/cli.ts– Reference command-line wrapper demonstrating end-to-end library usage.src/llm.ts– InternalcallLLMhelper that routes requests to the Claude Agent SDK.
Summary
- Import
runand the required types from"adhd"to embed the engine with minimal boilerplate. - Configure the cycle through the
RunOptionsinterface, specifying your problem, models, and tuning parameters. - The engine in
src/engine.tsexecutes a four-phase tree-of-thought cycle: Reframe, Diverge, Score + Cluster, and Deepen. - Consume the structured
RunResultprogrammatically or render it withrenderTextfor human review. - Because the library is model-agnostic at the configuration level, you can swap generator and critic models via
RunOptionswithout changing integration code.
Frequently Asked Questions
What is the minimum API surface needed to integrate ADHD into an existing agent?
You only need to import run and the relevant types such as RunOptions and RunResult from the package entry point. The run function defined in src/engine.ts encapsulates the entire reasoning cycle, so no additional classes or setup are required.
Does ADHD require a specific LLM provider or SDK?
The engine is model-agnostic in terms of which Claude model you specify in RunOptions, but the internal callLLM helper in src/llm.ts forwards requests to the Claude Agent SDK. Therefore, you must have the Claude Agent SDK available in your project.
How do I customize the cognitive frames used during the divergence phase?
You can import FRAMES and selectFrames from src/frames.ts to inspect or filter built-in frames, or you can adjust the framesPerRun parameter in your RunOptions to control how many frames fire per cycle.
Can I override the system prompts that drive the reasoning phases?
Yes. The system prompts—such as DIVERGE_SYSTEM, SCORE_SYSTEM, and DEEPEN_SYSTEM—are kept as constants in src/engine.ts. You can override them by patching the library source, or you can influence behavior by supplying different model and criticModel values in RunOptions.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →