caplyr 0.1.2 → 0.1.3
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/dist/index.d.mts +20 -141
- package/dist/index.d.ts +20 -141
- package/dist/index.js +219 -372
- package/dist/index.mjs +219 -372
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,102 +1,29 @@
|
|
|
1
|
-
/** Protection status of the SDK connection */
|
|
2
|
-
type ProtectionStatus = "ACTIVE" | "DEGRADED" | "OFF";
|
|
3
|
-
/** Supported AI providers */
|
|
4
|
-
type Provider = "anthropic" | "openai";
|
|
5
|
-
/** Operating mode for the SDK */
|
|
6
|
-
type CaplyrMode = "cost_protect" | "alert_only" | "kill_switch";
|
|
7
|
-
/** Configuration for the protect() function */
|
|
8
1
|
interface CaplyrConfig {
|
|
9
|
-
/** Your Caplyr project API key */
|
|
10
2
|
apiKey: string;
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
budget?: number;
|
|
17
|
-
/** Daily budget cap in dollars */
|
|
18
|
-
dailyBudget?: number;
|
|
19
|
-
/** Fallback model when budget is approached/exceeded */
|
|
20
|
-
fallback?: string;
|
|
21
|
-
/** Budget threshold (0-1) at which auto-downgrade activates (default: 0.8) */
|
|
3
|
+
mode?: 'alert_only' | 'cost_protect';
|
|
4
|
+
budget?: {
|
|
5
|
+
daily?: number;
|
|
6
|
+
monthly?: number;
|
|
7
|
+
};
|
|
22
8
|
downgradeThreshold?: number;
|
|
23
|
-
|
|
9
|
+
fallback?: string;
|
|
10
|
+
endpoint?: string;
|
|
24
11
|
endpoint_tag?: string;
|
|
25
|
-
/** Batch size before flushing logs (default: 10) */
|
|
26
12
|
batchSize?: number;
|
|
27
|
-
/** Max interval in ms between log flushes (default: 30000) */
|
|
28
13
|
flushInterval?: number;
|
|
29
|
-
/** Heartbeat interval in ms (default: 60000) */
|
|
30
14
|
heartbeatInterval?: number;
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
/** Called when an enforcement event occurs */
|
|
34
|
-
onEnforcement?: (event: EnforcementEvent) => void;
|
|
35
|
-
/** Called when an error occurs in the SDK (non-blocking) */
|
|
36
|
-
onError?: (error: Error) => void;
|
|
37
|
-
}
|
|
38
|
-
/** Metadata captured for every AI API call */
|
|
39
|
-
interface RequestLog {
|
|
40
|
-
/** Unique request ID */
|
|
41
|
-
id: string;
|
|
42
|
-
/** Timestamp of the request */
|
|
43
|
-
timestamp: number;
|
|
44
|
-
/** Provider: "anthropic" | "openai" */
|
|
45
|
-
provider: Provider;
|
|
46
|
-
/** Model used (e.g., "claude-sonnet-4-20250514") */
|
|
47
|
-
model: string;
|
|
48
|
-
/** Input tokens consumed */
|
|
49
|
-
input_tokens: number;
|
|
50
|
-
/** Output tokens consumed */
|
|
51
|
-
output_tokens: number;
|
|
52
|
-
/** Calculated cost in dollars */
|
|
53
|
-
cost: number;
|
|
54
|
-
/** Request latency in milliseconds */
|
|
55
|
-
latency_ms: number;
|
|
56
|
-
/** Endpoint tag for attribution */
|
|
57
|
-
endpoint_tag?: string;
|
|
58
|
-
/** Whether this request was auto-downgraded */
|
|
59
|
-
downgraded: boolean;
|
|
60
|
-
/** Original model if downgraded */
|
|
61
|
-
original_model?: string;
|
|
62
|
-
/** Whether this request was blocked */
|
|
63
|
-
blocked: boolean;
|
|
64
|
-
/** Reason for block/downgrade if applicable */
|
|
65
|
-
enforcement_reason?: string;
|
|
15
|
+
onError?: (err: Error) => void;
|
|
16
|
+
onEnforcement?: (event: any) => void;
|
|
66
17
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
timestamp: number;
|
|
71
|
-
reason: string;
|
|
72
|
-
original_model?: string;
|
|
73
|
-
fallback_model?: string;
|
|
74
|
-
budget_used: number;
|
|
75
|
-
budget_limit: number;
|
|
76
|
-
estimated_savings: number;
|
|
18
|
+
interface ResolvedConfig extends CaplyrConfig {
|
|
19
|
+
mode: 'alert_only' | 'cost_protect';
|
|
20
|
+
downgradeThreshold: number;
|
|
77
21
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
monthly_limit: number | null;
|
|
84
|
-
status: ProtectionStatus;
|
|
85
|
-
kill_switch_active: boolean;
|
|
86
|
-
}
|
|
87
|
-
/** Structured error returned when a request is blocked */
|
|
88
|
-
interface CaplyrBlockedError {
|
|
89
|
-
code: "BUDGET_EXCEEDED" | "KILL_SWITCH_ACTIVE" | "RATE_LIMITED";
|
|
90
|
-
message: string;
|
|
91
|
-
budget_used: number;
|
|
92
|
-
budget_limit: number;
|
|
93
|
-
retry_after?: string;
|
|
94
|
-
dashboard_url: string;
|
|
95
|
-
}
|
|
96
|
-
/** Internal state of the SDK */
|
|
97
|
-
interface CaplyrState {
|
|
98
|
-
status: ProtectionStatus;
|
|
99
|
-
mode: CaplyrMode;
|
|
22
|
+
declare function protect(client: any, config: CaplyrConfig): any;
|
|
23
|
+
declare function getStatus(apiKey: string): string;
|
|
24
|
+
declare function getState(apiKey: string): {
|
|
25
|
+
status: string;
|
|
26
|
+
mode: "alert_only" | "cost_protect";
|
|
100
27
|
budget_daily_used: number;
|
|
101
28
|
budget_monthly_used: number;
|
|
102
29
|
kill_switch_active: boolean;
|
|
@@ -104,65 +31,17 @@ interface CaplyrState {
|
|
|
104
31
|
request_count: number;
|
|
105
32
|
total_cost: number;
|
|
106
33
|
total_savings: number;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Wrap an AI provider client with Caplyr cost control.
|
|
111
|
-
*
|
|
112
|
-
* Returns a proxy that is type-compatible with the original client.
|
|
113
|
-
* All existing code works unchanged — Caplyr is invisible to callers.
|
|
114
|
-
*
|
|
115
|
-
* @param client - An Anthropic or OpenAI client instance
|
|
116
|
-
* @param config - Caplyr configuration
|
|
117
|
-
* @returns A proxied client with cost control applied
|
|
118
|
-
*
|
|
119
|
-
* @example
|
|
120
|
-
* ```ts
|
|
121
|
-
* import Anthropic from "@anthropic-ai/sdk"
|
|
122
|
-
* import { protect } from "caplyr"
|
|
123
|
-
*
|
|
124
|
-
* const client = protect(new Anthropic(), {
|
|
125
|
-
* apiKey: "caplyr_...",
|
|
126
|
-
* budget: 500,
|
|
127
|
-
* })
|
|
128
|
-
* ```
|
|
129
|
-
*/
|
|
130
|
-
declare function protect<T>(client: T, config: CaplyrConfig): T;
|
|
131
|
-
/**
|
|
132
|
-
* Get the current protection status for a given API key.
|
|
133
|
-
*/
|
|
134
|
-
declare function getStatus(apiKey: string): ProtectionStatus;
|
|
135
|
-
/**
|
|
136
|
-
* Get the full SDK state for a given API key.
|
|
137
|
-
*/
|
|
138
|
-
declare function getState(apiKey: string): CaplyrState | null;
|
|
139
|
-
/**
|
|
140
|
-
* Flush all pending logs and stop background tasks.
|
|
141
|
-
* Call this during application shutdown.
|
|
142
|
-
*/
|
|
34
|
+
} | null;
|
|
143
35
|
declare function shutdown(apiKey?: string): Promise<void>;
|
|
144
36
|
|
|
37
|
+
/** Per-million-token pricing: { input, output } */
|
|
145
38
|
interface ModelPricing {
|
|
146
39
|
input: number;
|
|
147
40
|
output: number;
|
|
148
41
|
}
|
|
149
|
-
/**
|
|
150
|
-
* Calculate the cost of a single API request.
|
|
151
|
-
* Returns cost in dollars.
|
|
152
|
-
*/
|
|
153
42
|
declare function calculateCost(model: string, inputTokens: number, outputTokens: number): number;
|
|
154
|
-
/**
|
|
155
|
-
* Register a custom model with pricing at runtime.
|
|
156
|
-
* Useful for new models not yet in the built-in table.
|
|
157
|
-
*/
|
|
158
43
|
declare function registerModel(model: string, pricing: ModelPricing, fallback?: string): void;
|
|
159
|
-
/**
|
|
160
|
-
* Check if a model is known to the pricing table.
|
|
161
|
-
*/
|
|
162
44
|
declare function isKnownModel(model: string): boolean;
|
|
163
|
-
/**
|
|
164
|
-
* Get pricing for a model. Returns null if unknown.
|
|
165
|
-
*/
|
|
166
45
|
declare function getModelPricing(model: string): ModelPricing | null;
|
|
167
46
|
|
|
168
|
-
export { type
|
|
47
|
+
export { type CaplyrConfig, type ResolvedConfig, calculateCost, getModelPricing, getState, getStatus, isKnownModel, protect, registerModel, shutdown };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,102 +1,29 @@
|
|
|
1
|
-
/** Protection status of the SDK connection */
|
|
2
|
-
type ProtectionStatus = "ACTIVE" | "DEGRADED" | "OFF";
|
|
3
|
-
/** Supported AI providers */
|
|
4
|
-
type Provider = "anthropic" | "openai";
|
|
5
|
-
/** Operating mode for the SDK */
|
|
6
|
-
type CaplyrMode = "cost_protect" | "alert_only" | "kill_switch";
|
|
7
|
-
/** Configuration for the protect() function */
|
|
8
1
|
interface CaplyrConfig {
|
|
9
|
-
/** Your Caplyr project API key */
|
|
10
2
|
apiKey: string;
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
budget?: number;
|
|
17
|
-
/** Daily budget cap in dollars */
|
|
18
|
-
dailyBudget?: number;
|
|
19
|
-
/** Fallback model when budget is approached/exceeded */
|
|
20
|
-
fallback?: string;
|
|
21
|
-
/** Budget threshold (0-1) at which auto-downgrade activates (default: 0.8) */
|
|
3
|
+
mode?: 'alert_only' | 'cost_protect';
|
|
4
|
+
budget?: {
|
|
5
|
+
daily?: number;
|
|
6
|
+
monthly?: number;
|
|
7
|
+
};
|
|
22
8
|
downgradeThreshold?: number;
|
|
23
|
-
|
|
9
|
+
fallback?: string;
|
|
10
|
+
endpoint?: string;
|
|
24
11
|
endpoint_tag?: string;
|
|
25
|
-
/** Batch size before flushing logs (default: 10) */
|
|
26
12
|
batchSize?: number;
|
|
27
|
-
/** Max interval in ms between log flushes (default: 30000) */
|
|
28
13
|
flushInterval?: number;
|
|
29
|
-
/** Heartbeat interval in ms (default: 60000) */
|
|
30
14
|
heartbeatInterval?: number;
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
/** Called when an enforcement event occurs */
|
|
34
|
-
onEnforcement?: (event: EnforcementEvent) => void;
|
|
35
|
-
/** Called when an error occurs in the SDK (non-blocking) */
|
|
36
|
-
onError?: (error: Error) => void;
|
|
37
|
-
}
|
|
38
|
-
/** Metadata captured for every AI API call */
|
|
39
|
-
interface RequestLog {
|
|
40
|
-
/** Unique request ID */
|
|
41
|
-
id: string;
|
|
42
|
-
/** Timestamp of the request */
|
|
43
|
-
timestamp: number;
|
|
44
|
-
/** Provider: "anthropic" | "openai" */
|
|
45
|
-
provider: Provider;
|
|
46
|
-
/** Model used (e.g., "claude-sonnet-4-20250514") */
|
|
47
|
-
model: string;
|
|
48
|
-
/** Input tokens consumed */
|
|
49
|
-
input_tokens: number;
|
|
50
|
-
/** Output tokens consumed */
|
|
51
|
-
output_tokens: number;
|
|
52
|
-
/** Calculated cost in dollars */
|
|
53
|
-
cost: number;
|
|
54
|
-
/** Request latency in milliseconds */
|
|
55
|
-
latency_ms: number;
|
|
56
|
-
/** Endpoint tag for attribution */
|
|
57
|
-
endpoint_tag?: string;
|
|
58
|
-
/** Whether this request was auto-downgraded */
|
|
59
|
-
downgraded: boolean;
|
|
60
|
-
/** Original model if downgraded */
|
|
61
|
-
original_model?: string;
|
|
62
|
-
/** Whether this request was blocked */
|
|
63
|
-
blocked: boolean;
|
|
64
|
-
/** Reason for block/downgrade if applicable */
|
|
65
|
-
enforcement_reason?: string;
|
|
15
|
+
onError?: (err: Error) => void;
|
|
16
|
+
onEnforcement?: (event: any) => void;
|
|
66
17
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
timestamp: number;
|
|
71
|
-
reason: string;
|
|
72
|
-
original_model?: string;
|
|
73
|
-
fallback_model?: string;
|
|
74
|
-
budget_used: number;
|
|
75
|
-
budget_limit: number;
|
|
76
|
-
estimated_savings: number;
|
|
18
|
+
interface ResolvedConfig extends CaplyrConfig {
|
|
19
|
+
mode: 'alert_only' | 'cost_protect';
|
|
20
|
+
downgradeThreshold: number;
|
|
77
21
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
monthly_limit: number | null;
|
|
84
|
-
status: ProtectionStatus;
|
|
85
|
-
kill_switch_active: boolean;
|
|
86
|
-
}
|
|
87
|
-
/** Structured error returned when a request is blocked */
|
|
88
|
-
interface CaplyrBlockedError {
|
|
89
|
-
code: "BUDGET_EXCEEDED" | "KILL_SWITCH_ACTIVE" | "RATE_LIMITED";
|
|
90
|
-
message: string;
|
|
91
|
-
budget_used: number;
|
|
92
|
-
budget_limit: number;
|
|
93
|
-
retry_after?: string;
|
|
94
|
-
dashboard_url: string;
|
|
95
|
-
}
|
|
96
|
-
/** Internal state of the SDK */
|
|
97
|
-
interface CaplyrState {
|
|
98
|
-
status: ProtectionStatus;
|
|
99
|
-
mode: CaplyrMode;
|
|
22
|
+
declare function protect(client: any, config: CaplyrConfig): any;
|
|
23
|
+
declare function getStatus(apiKey: string): string;
|
|
24
|
+
declare function getState(apiKey: string): {
|
|
25
|
+
status: string;
|
|
26
|
+
mode: "alert_only" | "cost_protect";
|
|
100
27
|
budget_daily_used: number;
|
|
101
28
|
budget_monthly_used: number;
|
|
102
29
|
kill_switch_active: boolean;
|
|
@@ -104,65 +31,17 @@ interface CaplyrState {
|
|
|
104
31
|
request_count: number;
|
|
105
32
|
total_cost: number;
|
|
106
33
|
total_savings: number;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Wrap an AI provider client with Caplyr cost control.
|
|
111
|
-
*
|
|
112
|
-
* Returns a proxy that is type-compatible with the original client.
|
|
113
|
-
* All existing code works unchanged — Caplyr is invisible to callers.
|
|
114
|
-
*
|
|
115
|
-
* @param client - An Anthropic or OpenAI client instance
|
|
116
|
-
* @param config - Caplyr configuration
|
|
117
|
-
* @returns A proxied client with cost control applied
|
|
118
|
-
*
|
|
119
|
-
* @example
|
|
120
|
-
* ```ts
|
|
121
|
-
* import Anthropic from "@anthropic-ai/sdk"
|
|
122
|
-
* import { protect } from "caplyr"
|
|
123
|
-
*
|
|
124
|
-
* const client = protect(new Anthropic(), {
|
|
125
|
-
* apiKey: "caplyr_...",
|
|
126
|
-
* budget: 500,
|
|
127
|
-
* })
|
|
128
|
-
* ```
|
|
129
|
-
*/
|
|
130
|
-
declare function protect<T>(client: T, config: CaplyrConfig): T;
|
|
131
|
-
/**
|
|
132
|
-
* Get the current protection status for a given API key.
|
|
133
|
-
*/
|
|
134
|
-
declare function getStatus(apiKey: string): ProtectionStatus;
|
|
135
|
-
/**
|
|
136
|
-
* Get the full SDK state for a given API key.
|
|
137
|
-
*/
|
|
138
|
-
declare function getState(apiKey: string): CaplyrState | null;
|
|
139
|
-
/**
|
|
140
|
-
* Flush all pending logs and stop background tasks.
|
|
141
|
-
* Call this during application shutdown.
|
|
142
|
-
*/
|
|
34
|
+
} | null;
|
|
143
35
|
declare function shutdown(apiKey?: string): Promise<void>;
|
|
144
36
|
|
|
37
|
+
/** Per-million-token pricing: { input, output } */
|
|
145
38
|
interface ModelPricing {
|
|
146
39
|
input: number;
|
|
147
40
|
output: number;
|
|
148
41
|
}
|
|
149
|
-
/**
|
|
150
|
-
* Calculate the cost of a single API request.
|
|
151
|
-
* Returns cost in dollars.
|
|
152
|
-
*/
|
|
153
42
|
declare function calculateCost(model: string, inputTokens: number, outputTokens: number): number;
|
|
154
|
-
/**
|
|
155
|
-
* Register a custom model with pricing at runtime.
|
|
156
|
-
* Useful for new models not yet in the built-in table.
|
|
157
|
-
*/
|
|
158
43
|
declare function registerModel(model: string, pricing: ModelPricing, fallback?: string): void;
|
|
159
|
-
/**
|
|
160
|
-
* Check if a model is known to the pricing table.
|
|
161
|
-
*/
|
|
162
44
|
declare function isKnownModel(model: string): boolean;
|
|
163
|
-
/**
|
|
164
|
-
* Get pricing for a model. Returns null if unknown.
|
|
165
|
-
*/
|
|
166
45
|
declare function getModelPricing(model: string): ModelPricing | null;
|
|
167
46
|
|
|
168
|
-
export { type
|
|
47
|
+
export { type CaplyrConfig, type ResolvedConfig, calculateCost, getModelPricing, getState, getStatus, isKnownModel, protect, registerModel, shutdown };
|