Skip to content

API Reference

Complete API documentation for all 18 colony-harness packages. Each package includes install commands, quick examples, and parameter tables.


Core Runtime

Core

colony-harness

Core runtime package. Contains HarnessBuilder, ColonyHarness, AgenticLoop, ToolRegistry, MemoryManager, TraceHub, Guardrails, and 5 built-in guards.

pnpm add colony-harness

HarnessBuilder

Declarative builder API — the single entry point. Chains LLM, tools, memory, tracing, and guards into a runtime.

typescript
import { HarnessBuilder } from 'colony-harness'

const harness = new HarnessBuilder()
  .llm(provider)
  .tool(calculatorTool)
  .memory(adapter)
  .trace(exporter)
  .guard(new PromptInjectionGuard())
  .build()
MethodParameterDescription
llm(provider)LLMProviderInject model caller (required)
tool(...tools)ToolDefinition[]Register tools
toolApproval(cb)(name, input) => Promise<boolean>High-risk tool approval callback
memory(adapter)MemoryAdapterSpecify memory backend
memoryConfig(cfg)See belowConfigure memory strategy
trace(...exporters)TraceExporter[]Configure trace exporters
guard(...guards)Guard[]Configure guards
loopConfig(cfg)See belowConfigure Agentic Loop behavior
systemPrompt(text)stringOverride default system prompt
build()Build and return ColonyHarness instance

loopConfig parameters:

FieldDefaultDescription
maxIterations20Maximum loop iterations
callTimeout30000Single model call timeout (ms)
modelFailStrategyabortModel fail strategy: abort / retry
modelRetryMax2Max model retries (when strategy is retry)
modelRetryBaseDelayMs300Base backoff delay for model retries (ms)
modelRetryMaxDelayMs5000Max backoff delay for model retries (ms)
modelRetryJitterRatio0.2Retry jitter ratio for model calls (0~1)
modelRetryMaxTotalDelayMs15000Max aggregate delay budget for model retries (ms)
modelCircuitBreakerEnabledtrueEnable model circuit breaker
modelCircuitBreakerFailureThreshold5Consecutive transient failures before opening circuit
modelCircuitBreakerCooldownMs30000Circuit breaker cooldown duration (ms)
maxTokensnoneTotal token limit
toolConcurrency1Tool concurrency level
toolFailStrategyabortFail strategy: abort / continue / retry
toolRetryMax2Max retries (when strategy is retry)

memoryConfig parameters:

FieldDefaultDescription
workingMemoryTokenLimit6000Working memory compression threshold
episodicRetentionDays30Episodic retention period in days
semanticTopK5Default semantic search result count
autoCompresstrueEnable automatic context compression
embeddernoneSemantic embedding function (text) => number[]

ColonyHarness

Runtime container. Registers task handlers, builds context, executes guards and tracing.

typescript
const harness = builder.build()

harness.task('chat', async (ctx) => {
  const result = await ctx.runLoop('User question')
  return result
})

const result = await harness.runTask('chat', { question: 'Hello' })
MethodParameterDescription
task(capability, handler)string, (ctx) => PromiseRegister task handler
runTask(capability, input, opts?)See belowExecute task

runTask options:

FieldDescription
taskIdExternal task ID (aligns with control plane)
agentIdAgent identifier
sessionIdSession identifier
signalAbortSignal for cancellation

HarnessContext

Context object available within task handlers.

typescript
harness.task('research', async (ctx) => {
  // Run Agent Loop
  const reply = await ctx.runLoop('Search for latest papers')

  // Invoke tool directly
  const calc = await ctx.invokeTool('calculator', { expression: '2+2' })

  // Memory operations
  await ctx.memory.save('key', { data: 'value' })
  const recent = await ctx.memory.recent(10)

  // Tracing
  const span = ctx.trace.startSpan('step')
  span.end()
})
Property/MethodTypeDescription
runLoop(prompt)string => Promise<string>Start Agentic Loop
invokeTool(name, input)(string, any) => Promise<any>Directly invoke a registered tool
callModel(messages)Message[] => Promise<ModelResponse>Directly call the LLM
callModelWithTools(msgs, tools)LLM call with tool definitionsCall LLM with tool list
memoryMemoryHandleMemory operation interface
traceTraceHandleTrace operation interface
inputanyTask input data

Built-in Guards

All guards are exported from the colony-harness package.

GuardDescriptionConfiguration
PromptInjectionGuardRegex-based injection detectionNone
PIIGuardRedact Chinese ID/phone/emailNone
TokenLimitGuardReject inputs exceeding token threshold{ maxTokens: number }
SensitiveWordGuardReject inputs with sensitive words{ words: string[] }
RateLimitGuardSliding window rate limiting{ windowMs, maxRequests }

Error Classes

Error ClassScenario
HarnessErrorBase error
ToolNotFoundErrorTool not registered
ToolInputValidationErrorTool input validation failed
ToolOutputValidationErrorTool output validation failed
ToolApprovalDeniedErrorTool approval denied
LoopMaxIterationsErrorMax iterations reached
LoopMaxTokensErrorMax tokens reached
GuardBlockedErrorBlocked by guard
MemoryAdapterErrorMemory adapter exception

LLM Providers

LLM

@colony-harness/llm-openai

OpenAI Chat Completions API adapter. Handles tool calling, token usage tracking, and timeout with AbortController.

pnpm add @colony-harness/llm-openai

typescript
import { OpenAIProvider } from '@colony-harness/llm-openai'

const provider = new OpenAIProvider({
  apiKey: process.env.OPENAI_API_KEY!,
  model: 'gpt-4o',
})
ParameterRequiredDefaultDescription
apiKeyYesOpenAI API Key
modelYesModel name
baseUrlNohttps://api.openai.com/v1Custom endpoint
temperatureNoSampling temperature
timeoutMsNoRequest timeout (ms)
headersNoAdditional request headers
LLM

@colony-harness/llm-anthropic

Anthropic Messages API adapter. Auto-separates system messages, maps tool_use format, normalizes stop_reason.

pnpm add @colony-harness/llm-anthropic

typescript
import { AnthropicProvider } from '@colony-harness/llm-anthropic'

const provider = new AnthropicProvider({
  apiKey: process.env.ANTHROPIC_API_KEY!,
  model: 'claude-sonnet-4-20250514',
})
ParameterRequiredDefaultDescription
apiKeyYesAnthropic API Key
modelYesClaude model name
baseUrlNohttps://api.anthropic.comCustom endpoint
maxTokensNoOutput token limit
temperatureNoSampling temperature
timeoutMsNoTimeout (ms)
anthropicVersionNo2023-06-01API version
headersNoAdditional headers
LLM

@colony-harness/llm-gemini

Google Gemini generateContent API adapter. Maps message roles and functionDeclarations, normalizes finishReason.

pnpm add @colony-harness/llm-gemini

typescript
import { GeminiProvider } from '@colony-harness/llm-gemini'

const provider = new GeminiProvider({
  apiKey: process.env.GEMINI_API_KEY!,
  model: 'gemini-1.5-pro',
})
ParameterRequiredDefaultDescription
apiKeyYesGemini API Key
modelYesModel name
baseUrlNoGemini API URLCustom endpoint
temperatureNoSampling temperature
timeoutMsNoTimeout (ms)
headersNoAdditional headers
LLM

@colony-harness/llm-openai-compatible

Universal adapter for any OpenAI Chat Completions-compatible endpoint. Extends OpenAIProvider. Ideal for domestic LLMs or self-hosted endpoints.

pnpm add @colony-harness/llm-openai-compatible

typescript
import { OpenAICompatibleProvider, createOpenAICompatibleProviderFromEnv } from '@colony-harness/llm-openai-compatible'

// Manual creation
const provider = new OpenAICompatibleProvider({
  apiKey: 'your-key',
  model: 'your-model',
  baseUrl: 'https://your-endpoint.com/v1',
})

// Create from environment variables
const provider2 = createOpenAICompatibleProviderFromEnv()
ParameterRequiredDescription
apiKeyYesAPI Key
modelYesModel name
baseUrlYesCompatible endpoint URL (required)
...othersSame as OpenAIProvider

All providers return a unified ModelResponse containing content, toolCalls, stopReason (completed | tool_calls | max_tokens | unknown), and usage.


Memory Adapters

Memory

@colony-harness/memory-sqlite

SQLite persistent memory adapter. Supports indexed agent+created_at queries, cosine similarity semantic search, session-based cleanup, and auto table creation.

pnpm add @colony-harness/memory-sqlite

typescript
import { SqliteMemoryAdapter } from '@colony-harness/memory-sqlite'

const memory = new SqliteMemoryAdapter({
  path: './data/agent-memory.db',
})
ParameterDefaultDescription
path./memory.dbSQLite database file path
Memory

@colony-harness/memory-redis

Redis memory adapter. Hash entries, sorted sets for time ordering, pipeline-optimized writes, JS-side cosine similarity search.

pnpm add @colony-harness/memory-redis

typescript
import { RedisMemoryAdapter } from '@colony-harness/memory-redis'

const memory = new RedisMemoryAdapter({
  url: process.env.REDIS_URL,
  namespace: 'my-agent',
})
ParameterDefaultDescription
urlREDIS_URL env varRedis connection URL
namespacecolony:memoryKey prefix
redisPass an existing ioredis instance

Trace Exporters

Trace

@colony-harness/trace-console

ANSI-colored terminal trace exporter. Displays TraceID, task info, duration, metrics, and span details.

pnpm add @colony-harness/trace-console

typescript
import { ConsoleTraceExporter } from '@colony-harness/trace-console'

const exporter = new ConsoleTraceExporter()

No configuration needed. Works out of the box.

Trace

@colony-harness/trace-file

JSONL file trace exporter. Appends completed traces to a configurable file path. Supports pretty-print JSON mode.

pnpm add @colony-harness/trace-file

typescript
import { FileTraceExporter } from '@colony-harness/trace-file'

const exporter = new FileTraceExporter({
  path: './logs/traces.jsonl',
  pretty: true,
})
ParameterDefaultDescription
path./traces.jsonlOutput file path
prettyfalseEnable pretty-print JSON
Trace

@colony-harness/trace-otel

OpenTelemetry bridge exporter. Converts traces to OTel spans, aligned with OpenInference semantics (openinference.span.kind, session.id, user.id, input/output.value and mime_type).

pnpm add @colony-harness/trace-otel

typescript
import { OpenTelemetryTraceExporter } from '@colony-harness/trace-otel'

const exporter = new OpenTelemetryTraceExporter()

Requires @opentelemetry/api SDK to be initialized first. Maps colony-harness spans as events on a root OTel span.

Trace

@colony-harness/trace-langfuse

Native Langfuse exporter. Sends traces and observations via the batch ingestion API. Supports custom fetch and tags.

pnpm add @colony-harness/trace-langfuse

typescript
import { LangfuseTraceExporter } from '@colony-harness/trace-langfuse'

const exporter = new LangfuseTraceExporter({
  publicKey: process.env.LANGFUSE_PUBLIC_KEY!,
  secretKey: process.env.LANGFUSE_SECRET_KEY!,
  baseUrl: 'https://cloud.langfuse.com',
})
ParameterRequiredDescription
publicKeyYesLangfuse Public Key
secretKeyYesLangfuse Secret Key
baseUrlNoLangfuse instance URL
fetchNoCustom fetch implementation
tagsNoAdditional tags

Built-in Tools

Tools

@colony-harness/tools-builtin

Eight built-in tools covering math, file ops, HTTP, shell commands, web search, JSON queries, and template rendering.

pnpm add @colony-harness/tools-builtin

Register all tools at once:

typescript
import { createBuiltinTools } from '@colony-harness/tools-builtin'

const allTools = createBuiltinTools({
  file: { baseDir: '/workspace', allowOutsideBaseDir: false },
  runCommand: { mode: 'allowlist', allowed: ['ls', 'cat', 'grep'] },
})

Individual Tool Details

calculatorTool — Safe math expression evaluation

typescript
import { calculatorTool } from '@colony-harness/tools-builtin'
// Input: { expression: "1 + 2 * 3" }
// Output: { result: 7 }

Strict character filtering — only numbers and basic operators allowed.

httpRequestTool — HTTP requests

typescript
// Input: { method: "GET", url: "https://api.example.com/data", headers: {} }
// Output: { status: 200, headers: {...}, body: "..." }

Configurable timeout and max body size.

createReadFileTool(options) — Read files

typescript
import { createReadFileTool } from '@colony-harness/tools-builtin'

const readFile = createReadFileTool({
  baseDir: '/workspace',
  allowOutsideBaseDir: false,
})

Path sandboxing prevents directory traversal attacks.

createWriteFileTool(options) — Write files

typescript
const writeFile = createWriteFileTool({
  baseDir: '/workspace',
})

Supports append mode and auto-creating directories.

createRunCommandTool(options) — Execute shell commands

typescript
import { createRunCommandTool } from '@colony-harness/tools-builtin'

const runCmd = createRunCommandTool({
  mode: 'allowlist',
  allowed: ['ls', 'cat', 'grep', 'node'],
  timeout: 30_000,
  approvalByRisk: {
    requiredFrom: 'medium',
    callback: async (cmd) => {
      return confirm(`Allow: ${cmd}?`)
    },
  },
})
ParameterDescription
mode'allowlist' or 'blocklist'
allowed / blockedCommand allowlist/blocklist
timeoutExecution timeout (ms)
approvalByRiskRisk-based approval configuration

Defaults to blocking dangerous commands like rm, sudo, dd. Returns an audit field for audit logging.

createSearchWebTool(provider?) — Web search

typescript
import { createSearchWebTool } from '@colony-harness/tools-builtin'

const search = createSearchWebTool() // Default: DuckDuckGo

Pluggable SearchWebProvider interface for other search engines.

jsonQueryTool — JSON queries

typescript
// Input: { data: { a: { b: [1, 2, 3] } }, path: "$.a.b[0]" }
// Output: { result: 1 }

JSONPath-like syntax ($.a.b[0].c). Read-only, no side effects.

templateRenderTool — Template rendering

typescript
// Input: { template: "Hello {{name}}, score is {{score}}", data: { name: "Alice", score: 95 } }
// Output: { result: "Hello Alice, score is 95" }

placeholder rendering. Read-only, no side effects.


Evaluation

Eval

@colony-harness/evals

Complete evaluation toolkit. Contains runEvalSuite runner, seven built-in Scorers, and evaluateGate quality gate.

pnpm add @colony-harness/evals

typescript
import { runEvalSuite, exactMatchScorer, evaluateGate } from '@colony-harness/evals'

const report = await runEvalSuite({
  cases: [
    { id: 'test-1', input: '1+1', expected: '2' },
  ],
  runner: async (input) => myAgent(input),
  scorer: exactMatchScorer(),
})

const gate = evaluateGate({ report, thresholds: { minPassRate: 0.95 } })

runEvalSuite

ParameterTypeDescription
casesEvalCase[]Test case dataset
runner(input) => Promise<any>Function being evaluated
scorerScorerScoring function
signalAbortSignalAbort signal
failFastbooleanStop on first failure

Built-in Scorers

ScorerPurposeConfiguration
exactMatchScorer()Exact JSON structural matchNone
containsScorer(opts)Contains expected phrases{ ignoreCase?, mode: 'all' | 'any' }
regexScorer(opts)Regex pattern matching{ flags? }
numericRangeScorer()Numeric range validation{ min?, max? }
llmJudgeScorer(opts)LLM-based subjective scoring{ judge, passThreshold }
safetyScorer(opts)Safety pattern checking{ blockedPatterns?, requiredPatterns? }
latencyScorer(opts)Latency scoring{ targetMs, maxMs }

evaluateGate

typescript
const gate = evaluateGate({
  report,
  thresholds: {
    minPassRate: 0.95,
    minWeightedScore: 0.85,
    maxLatencyMs: 5000,
  },
})
// gate.passed: boolean
// gate.failures: string[]

Control Plane

Control Plane

@colony-harness/controlplane-contract

Unified port contract for the control plane. Defines all interface types with no implementation.

pnpm add @colony-harness/controlplane-contract

Core exported types:

TypeDescription
ControlPlanePortControl plane interface: start(), stop(), onTaskAssign(), onTaskCancel(), reportProgress(), reportResult(), reportHealth()
TaskEnvelopeTask envelope (contains capability, input, etc.)
TaskResultEnvelopeTask result envelope
TaskErrorEnvelopeTask error envelope
TaskProgressEventProgress event
HealthStatusEventHealth status event
TaskExecutionMetricsExecution metrics
Control Plane

@colony-harness/controlplane-runtime

Runtime bridge. Connects ColonyHarness core with ControlPlanePort. Manages task lifecycle, registers handlers, tracks running tasks with AbortController for cancellation.

pnpm add @colony-harness/controlplane-runtime

typescript
import { HarnessControlPlaneRuntime } from '@colony-harness/controlplane-runtime'

const runtime = new HarnessControlPlaneRuntime(harness, controlPlaneAdapter)
await runtime.start()
Control Plane

@colony-harness/controlplane-mock-adapter

In-memory mock adapter. Stores progress events, results, and health events. Supports dispatchTask() for direct task injection in tests.

pnpm add @colony-harness/controlplane-mock-adapter

typescript
import { MockControlPlaneAdapter } from '@colony-harness/controlplane-mock-adapter'

const mock = new MockControlPlaneAdapter()
await mock.dispatchTask({ capability: 'chat', input: { msg: 'hello' } })
Control Plane

@colony-harness/controlplane-sdk-adapter

Queen SDK adapter. Connects to the Queen control plane via colony-bee-sdk. Uses duck-typed BeeAgentLike interface (no hard dependency). Supports dynamic SDK loading.

pnpm add @colony-harness/controlplane-sdk-adapter

typescript
import { BeeSDKControlPlaneAdapter, loadBeeAgentFromModule } from '@colony-harness/controlplane-sdk-adapter'

const adapter = new BeeSDKControlPlaneAdapter({
  colonyId: 'my-colony',
  capabilities: ['analyze', 'summarize'],
})

Exported utilities:

ExportDescription
BeeSDKControlPlaneAdapterQueen control plane adapter
loadBeeAgentFromModule(path)Dynamic SDK module loader
InMemoryBeeAgentStubIn-memory BeeAgent stub for testing

Released under the MIT License.