Skip to content

Agent

Provider-neutral coding agent. Each provider module exports a layer that provides Agent:

import * as ClaudeAgent from "@jpowersdev/effect-agent/claude"
program.pipe(Effect.provide(ClaudeAgent.layer()))

A session is a scoped resource: closing its Scope stops the agent process.

Since v0.1.0

Refuse a tool call from Hooks.beforeTool, with the reason the model gets as the tool’s result. An empty reason is replaced, as Codex needs one.

Signature

declare const refuseTool: (reason: string) => ToolRefusal

Source

Since v0.7.0

Whether a beforeTool result refuses the call.

Signature

declare const isToolRefusal: (u: unknown) => u is ToolRefusal

Source

Since v0.7.0

Sign-in and usage-limit status, from Agent.status.

Signature

declare const AccountStatus: Schema.Struct<{
readonly provider: Schema.String
readonly usable: Schema.Boolean
readonly reason: Schema.NullOr<
Schema.Literals<readonly ["unauthenticated", "usage_limited", "spend_limited"]>
>
readonly windows: Schema.$Array<
Schema.Struct<{
readonly window: Schema.Literals<readonly ["five_hour", "weekly", "other"]>
readonly label: Schema.String
readonly usedPercent: Schema.NullOr<Schema.Number>
readonly resetsAt: Schema.NullOr<Schema.DateTimeUtcFromMillis>
readonly model: Schema.OptionFromNullOr<Schema.String>
}>
>
readonly plan: Schema.NullOr<Schema.String>
readonly checkedAt: Schema.DateTimeUtcFromMillis
}>

Source

Since v0.1.0

Signature

type AccountStatus = typeof AccountStatus.Type

Source

Since v0.1.0

What Hooks.beforeTool returns: text to add to the model’s context (or none), or a ToolRefusal.

Signature

type BeforeToolResult = Option.Option<string> | ToolRefusal

Source

Since v0.7.0

What a provider supports, from Agent.capabilities.

Signature

declare const Capabilities: Schema.Struct<{
readonly provider: Schema.String
readonly version: Schema.NullOr<Schema.String>
readonly steer: Schema.Boolean
readonly queue: Schema.Boolean
readonly resume: Schema.Boolean
readonly cost: Schema.Literals<readonly ["reported", "estimated", "none"]>
readonly spendCap: Schema.Literals<readonly ["native", "estimated", "none"]>
readonly resetTimes: Schema.Boolean
readonly hooks: Schema.Boolean
readonly refuseTool: Schema.optionalKey<Schema.Boolean>
readonly reattach: Schema.Boolean
readonly disable: Schema.Struct<{
readonly askUser: Schema.Boolean
readonly subagents: Schema.Boolean
readonly tools: Schema.Literals<readonly ["any", "some"]>
}>
}>

Source

Since v0.1.0

Signature

type Capabilities = typeof Capabilities.Type

Source

Since v0.1.0

Reasoning effort for a turn.

Signature

declare const Effort: Schema.Literals<
readonly ["minimal", "low", "medium", "high", "xhigh", "max"]
>

Source

Since v0.1.0

Signature

type Effort = typeof Effort.Type

Source

Since v0.1.0

Each hook may return text that is added to the model’s context. Hooks run inside the agent’s tool loop, so keep them fast; a failing hook adds nothing.

beforeTool may instead refuse the call (refuseTool): the tool doesn’t run, and the model gets the reason as its result. A refusal holds with permissions bypassed, as for mcp. It fails open: a hook that fails, times out, or (on a detached session) has no client to answer it refuses nothing. Where capabilities.refuseTool isn’t true, as on a host from an older release, a refusal is only added to the model’s context, and the tool runs.

Signature

export interface Hooks {
readonly beforeTool?: (call: ToolHookInput) => Effect.Effect<BeforeToolResult>
readonly afterTool?: (
call: ToolHookInput & { readonly output: string; readonly isError: boolean },
) => Effect.Effect<Option.Option<string>>
/**
* The agent is about to end its turn. Returning text keeps the turn going with that
* text as context.
*/
readonly beforeStop?: () => Effect.Effect<Option.Option<string>>
}

Source

Since v0.1.0

An MCP server for RunSettings.mcpServers: Streamable HTTP, or a command speaking MCP on stdio.

Signature

declare const McpServer: Schema.Union<
readonly [
Schema.Struct<{
readonly type: Schema.Literal<"http">
readonly url: Schema.String
readonly headers: Schema.optionalKey<Schema.$Record<Schema.String, Schema.String>>
}>,
Schema.Struct<{
readonly type: Schema.Literal<"stdio">
readonly command: Schema.String
readonly args: Schema.optionalKey<Schema.$Array<Schema.String>>
readonly env: Schema.optionalKey<Schema.$Record<Schema.String, Schema.String>>
}>,
]
>

Source

Since v0.8.0

Signature

type McpServer = typeof McpServer.Type

Source

Since v0.8.0

Options for Agent.open: RunSettings, plus hooks.

Signature

export interface RunOptions extends RunSettings {
/**
* Callbacks that can add context to the turn. Only where `capabilities.hooks` is
* true.
*/
readonly hooks?: Hooks
}

Source

Since v0.1.0

Everything in RunOptions but hooks, as a Schema: plain data that can be stored or sent to another process (DetachedAgent sends it to its host). Durations encode as millis; an env value of undefined as null.

Signature

declare const RunSettings: Schema.Struct<{
readonly cwd: Schema.String
readonly env: Schema.optionalKey<
Schema.decodeTo<
Schema.$Record<Schema.String, Schema.UndefinedOr<Schema.String>>,
Schema.$Record<Schema.String, Schema.NullOr<Schema.String>>,
never,
never
>
>
readonly model: Schema.optionalKey<Schema.String>
readonly effort: Schema.optionalKey<
Schema.Literals<readonly ["minimal", "low", "medium", "high", "xhigh", "max"]>
>
readonly appendSystemPrompt: Schema.optionalKey<Schema.String>
readonly disable: Schema.optionalKey<
Schema.Struct<{
readonly askUser: Schema.optionalKey<Schema.Boolean>
readonly subagents: Schema.optionalKey<Schema.Boolean>
readonly tools: Schema.optionalKey<Schema.$Array<Schema.String>>
}>
>
readonly mcp: Schema.optionalKey<
Schema.Struct<{
readonly allow: Schema.$Array<Schema.String>
readonly deny: Schema.optionalKey<Schema.$Array<Schema.String>>
}>
>
readonly mcpServers: Schema.optionalKey<
Schema.$Record<
Schema.String,
Schema.Union<
readonly [
Schema.Struct<{
readonly type: Schema.Literal<"http">
readonly url: Schema.String
readonly headers: Schema.optionalKey<
Schema.$Record<Schema.String, Schema.String>
>
}>,
Schema.Struct<{
readonly type: Schema.Literal<"stdio">
readonly command: Schema.String
readonly args: Schema.optionalKey<Schema.$Array<Schema.String>>
readonly env: Schema.optionalKey<
Schema.$Record<Schema.String, Schema.String>
>
}>,
]
>
>
>
readonly spendCapUsd: Schema.optionalKey<Schema.Number>
readonly resume: Schema.optionalKey<
Schema.brand<Schema.String, "effect-agent/SessionId">
>
readonly watchdog: Schema.optionalKey<
Schema.Struct<{
readonly idle: Schema.decodeTo<
Schema.declare<Input, Input>,
Schema.Union<readonly [Schema.String, Schema.Number]>,
never,
never
>
readonly toolIdle: Schema.optionalKey<
Schema.decodeTo<
Schema.declare<Input, Input>,
Schema.Union<readonly [Schema.String, Schema.Number]>,
never,
never
>
>
readonly grace: Schema.optionalKey<
Schema.decodeTo<
Schema.declare<Input, Input>,
Schema.Union<readonly [Schema.String, Schema.Number]>,
never,
never
>
>
}>
>
}>

Source

Since v0.2.0

Signature

type RunSettings = typeof RunSettings.Type

Source

Since v0.2.0

Options for Agent.status.

Signature

export interface StatusOptions {
/**
* Applied over the host environment like `RunOptions.env`, e.g. to check
* another account: `{ CLAUDE_CONFIG_DIR: "..." }` or `{ CODEX_HOME: "..." }`.
*/
readonly env?: Readonly<Record<string, string | undefined>>
}

Source

Since v0.1.0

The tool call passed to Hooks.beforeTool and Hooks.afterTool.

Signature

export interface ToolHookInput {
readonly callId: string
readonly name: string
readonly input: unknown
}

Source

Since v0.1.0

What Hooks.beforeTool returns to stop a call: the tool doesn’t run, and the model gets reason as its result. Make one with refuseTool.

Signature

export interface ToolRefusal {
readonly _tag: "ToolRefusal"
readonly reason: string
}

Source

Since v0.7.0

Whether a limit window applies to model: windows without a model apply to all; a per-model window applies when the model id contains its family name (e.g. claude-fable-5-1 and fable).

Example

import { appliesTo } from "@jpowersdev/effect-agent"
import { Option } from "effect"
import assert from "node:assert"
const weeklyFable = {
window: "weekly",
label: "seven_day_fable",
usedPercent: 100,
resetsAt: null,
model: Option.some("fable"),
} as const
assert.strictEqual(appliesTo(weeklyFable, "claude-fable-5-1"), true)
assert.strictEqual(appliesTo(weeklyFable, "claude-sonnet-5-5"), false)

Signature

declare const appliesTo: (window: LimitWindowStatus, model: string) => boolean

Source

Since v0.1.0

status.usable, and no exhausted per-model window caps model. A full per-model window doesn’t make the whole account unusable (other models still run), but it does stop a run on that model.

Signature

declare const usableFor: (status: AccountStatus, model: string) => boolean

Source

Since v0.1.0

The provider-neutral agent service; each provider module’s layer provides it.

Example

import { Agent } from "@jpowersdev/effect-agent"
import { ClaudeAgent, ClaudeSdk } from "@jpowersdev/effect-agent/claude"
import { Effect, Layer, Stream } from "effect"
const program = Effect.gen(function* () {
const agent = yield* Agent
const session = yield* agent.open({ cwd: process.cwd(), spendCapUsd: 5 })
// Subscribe before sending, so no event is missed
const events = yield* session.subscribe
yield* events.pipe(Stream.runForEach(Effect.logInfo), Effect.forkScoped)
return yield* session.turn("Fix the failing test")
}).pipe(
// Closing the scope ends the session and stops the agent process
Effect.scoped,
Effect.provide(ClaudeAgent.layer().pipe(Layer.provide(ClaudeSdk.layer()))),
)

Signature

declare class Agent

Source

Since v0.1.0

RunOptions without its hooks (functions can’t leave the process).

Signature

declare const settingsOf: ({ hooks: _, ...settings }: RunOptions) => RunSettings

Source

Since v0.2.0