@knpkv/effect-ai-claude-agent-sdk
v0.3.0
Published
Effect-TS wrapper for Anthropic Claude Agent SDK with @effect/ai integration
Downloads
11
Maintainers
Readme
@knpkv/effect-ai-claude-agent-sdk
Effect-TS wrapper for the Anthropic Claude Agent SDK with type-safe integration.
Installation
pnpm add @knpkv/effect-ai-claude-agent-sdk @anthropic-ai/claude-agent-sdk effectAuthentication
ANTHROPIC_API_KEY is optional if running on an authenticated machine with Claude Code Pro/Max subscription. The SDK will use authentication automatically.
For unauthenticated environments, set your API key:
export ANTHROPIC_API_KEY=your-key-hereGet your API key from: https://console.anthropic.com/
Quick Start
import { Effect } from "effect"
import * as AgentClient from "@knpkv/effect-ai-claude-agent-sdk"
const program = Effect.gen(function* () {
const client = yield* AgentClient.ClaudeAgentClient.ClaudeAgentClient
const result = yield* client.queryText({
prompt: "What is Effect?"
})
console.log(result)
})
Effect.runPromise(program.pipe(Effect.provide(AgentClient.ClaudeAgentClient.layer())))Features
- ✅ Type-safe Effect wrapper for Claude Agent SDK
- ✅ Stream-based message handling with backpressure
- ✅ Service-based architecture with Layer composition
- ✅ Comprehensive error handling with tagged errors
- ✅ Full TypeScript support with strict mode
- ✅ JSDoc documentation for all public APIs
- ✅ Type-safe tool names with IDE autocomplete
- ✅ Token usage tracking (per-message and aggregate)
Architecture
The package follows Effect-TS best practices:
- Services:
ClaudeAgentClient,ClaudeAgentConfig - Layers: Dependency injection via Effect Layer
- Streams: Backpressure-aware message streaming
- Errors: Tagged errors for type-safe error handling
Configuration
import * as AgentClient from "@knpkv/effect-ai-claude-agent-sdk"
import * as AgentConfig from "@knpkv/effect-ai-claude-agent-sdk/ClaudeAgentConfig"
const configLayer = AgentConfig.layer({
apiKeySource: "project",
workingDirectory: "/my/project",
allowedTools: ["Read", "Write", "Edit"] // IDE autocomplete for known tools
})
const program = Effect.gen(function* () {
const client = yield* AgentClient.ClaudeAgentClient.ClaudeAgentClient
// Use client...
})
Effect.runPromise(
program.pipe(Effect.provide(AgentClient.ClaudeAgentClient.layerConfig()), Effect.provide(configLayer))
)API Reference
ClaudeAgentClient
Main client service for executing queries.
query(options): Execute query and return converted message stream (MessageEvent)queryRaw(options): Execute query and return raw SDK message stream (unconverted)queryText(options): Execute query and collect text response
ClaudeAgentConfig
Configuration service for customizing SDK behavior.
layer(options): Create config layer with optionsmake(options): Create config service
Error Types
SdkError: SDK initialization or execution errorsStreamError: Message streaming failuresToolError: Tool execution failuresValidationError: Input validation failuresPermissionError: Tool permission denials
Examples
See the examples/ directory for comprehensive usage patterns:
Basic Usage
basic-query.ts- Simple query execution and text responsestreaming.ts- Stream message-by-message processingstream-processing.ts- Advanced stream filtering and transformationchunk-logging.ts- Detailed chunk logging with tool callstools.ts- Using SDK with allowed tools for enhanced capabilitiesdeny-all-tools.ts- Empty allowedTools array (demonstrates limitation)token-usage.ts- Track token usage and API costs
Error Handling & Resilience
error-handling.ts- Typed error recovery with catchTagretry-resilience.ts- Retry logic and timeout patterns
Configuration & Composition
configuration.ts- Custom configuration with layersservice-composition.ts- Building services on top of ClaudeAgentClient
@effect/ai Integration
language-model.ts- LanguageModel integration with generateText and streamText
Running Examples
# Install dependencies
pnpm install
# Run an example
# Note: ANTHROPIC_API_KEY is optional on authenticated machines
# (Claude Code Pro/Max subscription)
npx tsx examples/basic-query.ts
npx tsx examples/streaming.ts
npx tsx examples/error-handling.ts
npx tsx examples/chunk-logging.ts
npx tsx examples/tools.ts
npx tsx examples/language-model.tsIntegration Tests
Integration tests verify compatibility with the latest @anthropic-ai/claude-agent-sdk:
# Run integration tests
# Note: ANTHROPIC_API_KEY is optional on authenticated machines
# (Claude Code Pro/Max subscription). Otherwise:
export ANTHROPIC_API_KEY=your-key-here
pnpm test:integrationThese tests run automatically:
- Twice daily via GitHub Actions (9-10 AM and 6-7 PM CET)
- On demand via workflow dispatch
- On PRs that modify integration tests or SDK packages
If a test fails, it indicates a potential breaking change in the Anthropic SDK.
Known Limitations
This package is actively developed. Current limitations:
Not Yet Implemented
- Lifecycle Hooks: The
ClaudeAgentHookmodule provides type definitions for lifecycle hooks (SessionStart, PreToolUse, etc.), but hook execution is not implemented. Hooks defined in query options will not be called.
Maintenance Requirements
- Tool Synchronization: The
Tool.allToolsarray must be manually kept in sync with tools added to the Claude Agent SDK. When new tools are added upstream, they should be added toClaudeAgentTool.ts.
API Compatibility
- SDK Dependency: This package wraps
@anthropic-ai/claude-agent-sdkand may lag behind SDK updates. Integration tests run twice daily to detect breaking changes.
License
MIT
Author
knpkv
