skalpel 1.1.2 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,196 +1,116 @@
1
- # Skalpel
1
+ # Skalpel SDK
2
2
 
3
- Optimize Claude Code prompts to reduce credit usage. Skalpel sits between Claude Code and the Anthropic API, optimizing prompts before they reach the providersaving you money without changing how you work.
3
+ Optimize your OpenAI and Anthropic API calls with Skalpel AI. The SDK transparently reroutes requests through the Skalpel proxy, adding cost optimization, caching, and intelligent model routing — without changing your existing code.
4
4
 
5
- ## How It Works
5
+ ## Installation
6
6
 
7
+ ```bash
8
+ npm install skalpel
7
9
  ```
8
- Claude Code --> Skalpel Proxy (local) --> Skalpel Backend (AWS) --> Anthropic API
9
- Intercepts requests Optimizes prompts Returns response
10
- Adds tracking headers Reduces token usage
11
- ```
12
-
13
- 1. A local proxy runs on your machine (port 18100)
14
- 2. Claude Code is configured to send API requests to the proxy instead of `api.anthropic.com`
15
- 3. The proxy forwards requests to the Skalpel backend, which optimizes prompts for lower token usage
16
- 4. The optimized request is sent to Anthropic, and the response streams back to Claude Code
17
-
18
- Your Anthropic API key stays in the request chain — Skalpel never stores it.
19
10
 
20
11
  ## Quick Start
21
12
 
22
- One command to install, detect Claude Code, and start optimizing:
13
+ ### Wrapper Pattern
23
14
 
24
- ```bash
25
- npx skalpel --api-key sk-skalpel-YOUR_KEY --auto
26
- ```
15
+ Wrap your existing client to route all calls through Skalpel with automatic fallback:
27
16
 
28
- This will:
29
- - Start the local proxy on ports 18100 (Anthropic) and 18101 (OpenAI)
30
- - Detect Claude Code on your machine
31
- - Configure `~/.claude/settings.json` with `ANTHROPIC_BASE_URL=http://localhost:18100`
32
- - Set shell environment variables in your `.bashrc`/`.zshrc`
33
- - Begin optimizing all Claude Code API traffic immediately
34
-
35
- ### Interactive Setup
17
+ ```typescript
18
+ import OpenAI from 'openai';
19
+ import { createSkalpelClient } from 'skalpel';
36
20
 
37
- For a guided setup with prompts:
21
+ const openai = createSkalpelClient(new OpenAI(), {
22
+ apiKey: process.env.SKALPEL_API_KEY!,
23
+ workspace: 'my-workspace',
24
+ });
38
25
 
39
- ```bash
40
- npx skalpel
26
+ // Use exactly like the OpenAI client
27
+ const response = await openai.chat.completions.create({
28
+ model: 'gpt-4o',
29
+ messages: [{ role: 'user', content: 'Hello' }],
30
+ });
41
31
  ```
42
32
 
43
- The wizard walks you through API key entry, agent detection, and proxy configuration.
44
-
45
- ### Manual Setup
46
-
47
- ```bash
48
- # 1. Start the proxy
49
- npx skalpel start
50
-
51
- # 2. Run the setup wizard to configure Claude Code
52
- npx skalpel setup
53
-
54
- # 3. Verify everything is working
55
- npx skalpel doctor
56
- ```
33
+ ### URL Swap Pattern
57
34
 
58
- ## What Gets Configured
35
+ The simplest integration — one line change:
59
36
 
60
- ### Claude Code (`~/.claude/settings.json`)
37
+ ```typescript
38
+ import { createSkalpelOpenAI } from 'skalpel';
61
39
 
62
- Skalpel adds an environment override so Claude Code routes API calls through the local proxy:
40
+ const openai = await createSkalpelOpenAI({
41
+ apiKey: process.env.SKALPEL_API_KEY!,
42
+ });
63
43
 
64
- ```json
65
- {
66
- "env": {
67
- "ANTHROPIC_BASE_URL": "http://localhost:18100"
68
- }
69
- }
44
+ const response = await openai.chat.completions.create({
45
+ model: 'gpt-4o',
46
+ messages: [{ role: 'user', content: 'Hello' }],
47
+ });
70
48
  ```
71
49
 
72
- ### Shell Environment (`~/.bashrc`, `~/.zshrc`)
73
-
74
- Environment variables are added for tools that read them directly:
50
+ ## CLI Setup
75
51
 
76
52
  ```bash
77
- # BEGIN SKALPEL PROXY - do not edit manually
78
- export ANTHROPIC_BASE_URL="http://localhost:18100"
79
- export OPENAI_BASE_URL="http://localhost:18101"
80
- # END SKALPEL PROXY
81
- ```
82
-
83
- ### Proxy Config (`~/.skalpel/config.json`)
84
-
85
- ```json
86
- {
87
- "apiKey": "sk-skalpel-YOUR_KEY",
88
- "remoteBaseUrl": "http://skalpel-production-554359744.us-west-2.elb.amazonaws.com",
89
- "anthropicPort": 18100,
90
- "openaiPort": 18101
91
- }
53
+ npx skalpel init
92
54
  ```
93
55
 
94
- ## CLI Commands
95
-
96
- | Command | Description |
97
- |---|---|
98
- | `npx skalpel` | Run the setup wizard |
99
- | `npx skalpel start` | Start the proxy |
100
- | `npx skalpel stop` | Stop the proxy |
101
- | `npx skalpel status` | Show proxy status and uptime |
102
- | `npx skalpel doctor` | Verify configuration and connectivity |
103
- | `npx skalpel logs` | View proxy logs |
104
- | `npx skalpel logs -f` | Follow proxy logs in real time |
105
- | `npx skalpel uninstall` | Remove proxy, configs, and shell modifications |
56
+ Interactive wizard that detects your project type, AI SDKs, and generates the integration code.
106
57
 
107
- ### Flags
108
-
109
- | Flag | Description |
110
- |---|---|
111
- | `--api-key <key>` | Provide Skalpel API key (skips interactive prompt) |
112
- | `--auto` | Non-interactive mode (requires `--api-key`) |
113
-
114
- ## Streaming Support
115
-
116
- Skalpel fully supports Anthropic's SSE streaming protocol. When Claude Code sends a streaming request (`"stream": true`), the proxy:
117
-
118
- - Detects the streaming flag in the request body
119
- - Forwards to the backend with all original headers
120
- - Pipes SSE events (`message_start`, `content_block_delta`, `message_stop`) directly back to Claude Code
121
- - Preserves upstream headers like `anthropic-request-id`
122
-
123
- No buffering, no modification of the stream — chunks flow through in real time.
124
-
125
- ## Headers
126
-
127
- The proxy adds these headers to every request forwarded to the Skalpel backend:
128
-
129
- | Header | Value | Purpose |
130
- |---|---|---|
131
- | `X-Skalpel-API-Key` | Your Skalpel key | Backend authentication |
132
- | `X-Skalpel-Source` | `claude-code` | Traffic source identification |
133
- | `X-Skalpel-Agent-Type` | `claude-code` | Agent type for optimization routing |
134
- | `X-Skalpel-SDK-Version` | `proxy-1.0.0` | SDK version tracking |
135
-
136
- The original `x-api-key` header (your Anthropic key) is forwarded as-is.
137
-
138
- ## Codex Support
139
-
140
- Skalpel also supports OpenAI Codex on port 18101. The same `--auto` flow detects and configures both agents if present.
141
-
142
- ## SDK Integration
58
+ ## API Reference
143
59
 
144
- For programmatic use in your own applications:
60
+ ### `createSkalpelClient<T>(client: T, options: SkalpelClientOptions): T`
145
61
 
146
- ```typescript
147
- import { createSkalpelClient } from 'skalpel';
148
- import Anthropic from '@anthropic-ai/sdk';
62
+ Wraps an existing OpenAI or Anthropic client instance. All API calls are transparently routed through the Skalpel proxy. Supports automatic fallback to the direct provider on errors.
149
63
 
150
- const client = createSkalpelClient(new Anthropic(), {
151
- apiKey: process.env.SKALPEL_API_KEY!,
152
- });
64
+ ### `createSkalpelOpenAI(options): Promise<OpenAI>`
153
65
 
154
- const response = await client.messages.create({
155
- model: 'claude-sonnet-4-20250514',
156
- max_tokens: 1024,
157
- messages: [{ role: 'user', content: 'Hello' }],
158
- });
159
- ```
66
+ Factory function that creates a pre-configured OpenAI client pointing at the Skalpel proxy.
160
67
 
161
- See the [API Reference](#api-reference) for all SDK options.
68
+ ### `createSkalpelAnthropic(options): Promise<Anthropic>`
162
69
 
163
- ## API Reference
70
+ Factory function that creates a pre-configured Anthropic client pointing at the Skalpel proxy.
164
71
 
165
- ### `createSkalpelClient<T>(client: T, options: SkalpelClientOptions): T`
72
+ ### `extractMetadata(headers): SkalpelMetadata | null`
166
73
 
167
- Wraps an existing OpenAI or Anthropic client. All API calls route through the Skalpel proxy with automatic fallback on errors.
74
+ Extracts optimization metadata from Skalpel response headers (`x-skalpel-*`).
168
75
 
169
- ### `createSkalpelAnthropic(options): Promise<Anthropic>`
76
+ ## Error Handling
170
77
 
171
- Creates a pre-configured Anthropic client pointing at the Skalpel proxy.
78
+ The SDK provides typed error classes:
172
79
 
173
- ### `createSkalpelOpenAI(options): Promise<OpenAI>`
80
+ - `SkalpelAuthError` (401) — authentication failed. Never falls back.
81
+ - `SkalpelTimeoutError` — request timed out.
82
+ - `SkalpelRateLimitError` (429) — rate limit exceeded, includes `retryAfter`.
83
+ - `SkalpelUnavailableError` (5xx) — proxy unavailable.
174
84
 
175
- Creates a pre-configured OpenAI client pointing at the Skalpel proxy.
85
+ When `fallbackOnError: true` (default), the SDK retries with exponential backoff, then falls back to the direct provider. Auth errors always throw immediately.
176
86
 
177
- ### Configuration Options
87
+ ## Configuration
178
88
 
179
89
  ```typescript
180
90
  interface SkalpelClientOptions {
181
91
  apiKey: string; // Skalpel API key (sk-skalpel-*)
182
92
  workspace?: string; // Workspace ID
183
- baseURL?: string; // Proxy URL (default: production ALB)
93
+ baseURL?: string; // Proxy URL (default: https://api.skalpel.ai)
184
94
  fallbackOnError?: boolean; // Fall back to direct provider (default: true)
185
95
  timeout?: number; // Request timeout in ms (default: 30000)
186
96
  retries?: number; // Max retries (default: 2)
97
+ headers?: Record<string, string>;// Additional custom headers
98
+ verbose?: boolean; // Log fallback events (default: false)
99
+ onFallback?: (error, provider) => void; // Callback on fallback
100
+ onMetadata?: (metadata) => void; // Callback with optimization info
187
101
  }
188
102
  ```
189
103
 
190
- ## Uninstall
104
+ ## TypeScript
191
105
 
192
- ```bash
193
- npx skalpel uninstall
194
- ```
106
+ The SDK is written in TypeScript and ships with full type declarations. All types are exported:
195
107
 
196
- This removes the proxy, restores `~/.claude/settings.json` from backup, and cleans up shell environment variables.
108
+ ```typescript
109
+ import type {
110
+ SkalpelClientOptions,
111
+ SkalpelMetadata,
112
+ SkalpelConfig,
113
+ SupportedProvider,
114
+ InitConfig,
115
+ } from 'skalpel';
116
+ ```