Skip to content

Commit 22c4047

Browse files
committed
feat(api): add new usage response fields (#281)
1 parent fcac7f0 commit 22c4047

File tree

6 files changed

+221
-156
lines changed

6 files changed

+221
-156
lines changed

api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ Types:
2222
- <code><a href="./src/resources/beta/messages.ts">ContentBlockStopEvent</a></code>
2323
- <code><a href="./src/resources/beta/messages.ts">Message</a></code>
2424
- <code><a href="./src/resources/beta/messages.ts">MessageDeltaEvent</a></code>
25+
- <code><a href="./src/resources/beta/messages.ts">MessageDeltaUsage</a></code>
2526
- <code><a href="./src/resources/beta/messages.ts">MessageParam</a></code>
2627
- <code><a href="./src/resources/beta/messages.ts">MessageStartEvent</a></code>
2728
- <code><a href="./src/resources/beta/messages.ts">MessageStopEvent</a></code>
2829
- <code><a href="./src/resources/beta/messages.ts">MessageStreamEvent</a></code>
2930
- <code><a href="./src/resources/beta/messages.ts">TextBlock</a></code>
3031
- <code><a href="./src/resources/beta/messages.ts">TextDelta</a></code>
32+
- <code><a href="./src/resources/beta/messages.ts">Usage</a></code>
3133

3234
Methods:
3335

src/resources/beta/beta.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ export namespace Beta {
1515
export import ContentBlockStopEvent = MessagesAPI.ContentBlockStopEvent;
1616
export import Message = MessagesAPI.Message;
1717
export import MessageDeltaEvent = MessagesAPI.MessageDeltaEvent;
18+
export import MessageDeltaUsage = MessagesAPI.MessageDeltaUsage;
1819
export import MessageParam = MessagesAPI.MessageParam;
1920
export import MessageStartEvent = MessagesAPI.MessageStartEvent;
2021
export import MessageStopEvent = MessagesAPI.MessageStopEvent;
2122
export import MessageStreamEvent = MessagesAPI.MessageStreamEvent;
2223
export import TextBlock = MessagesAPI.TextBlock;
2324
export import TextDelta = MessagesAPI.TextDelta;
25+
export import Usage = MessagesAPI.Usage;
2426
export import MessageCreateParams = MessagesAPI.MessageCreateParams;
2527
export import MessageCreateParamsNonStreaming = MessagesAPI.MessageCreateParamsNonStreaming;
2628
export import MessageCreateParamsStreaming = MessagesAPI.MessageCreateParamsStreaming;

src/resources/beta/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ export {
88
ContentBlockStopEvent,
99
Message,
1010
MessageDeltaEvent,
11+
MessageDeltaUsage,
1112
MessageParam,
1213
MessageStartEvent,
1314
MessageStopEvent,
1415
MessageStreamEvent,
1516
TextBlock,
1617
TextDelta,
18+
Usage,
1719
MessageCreateParams,
1820
MessageCreateParamsNonStreaming,
1921
MessageCreateParamsStreaming,

src/resources/beta/messages.ts

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@ export class Messages extends APIResource {
1212
/**
1313
* Create a Message.
1414
*
15-
* The Messages API is currently in beta.
15+
* Send a structured list of input messages, and the model will generate the next
16+
* message in the conversation.
17+
*
18+
* Messages can be used for either single queries to the model or for multi-turn
19+
* conversations.
20+
*
21+
* The Messages API is currently in beta. During beta, you must send the
22+
* `anthropic-beta: messages-2023-12-15` header in your requests. If you are using
23+
* our client SDKs, this is handled for you automatically.
1624
*/
1725
create(body: MessageCreateParamsNonStreaming, options?: Core.RequestOptions): APIPromise<Message>;
1826
create(
@@ -156,12 +164,22 @@ export interface Message {
156164
stop_sequence: string | null;
157165

158166
type: 'message';
167+
168+
/**
169+
* Container for the number of tokens used.
170+
*/
171+
usage: Usage;
159172
}
160173

161174
export interface MessageDeltaEvent {
162175
delta: MessageDeltaEvent.Delta;
163176

164177
type: 'message_delta';
178+
179+
/**
180+
* Container for the number of tokens used.
181+
*/
182+
usage: MessageDeltaUsage;
165183
}
166184

167185
export namespace MessageDeltaEvent {
@@ -172,6 +190,13 @@ export namespace MessageDeltaEvent {
172190
}
173191
}
174192

193+
export interface MessageDeltaUsage {
194+
/**
195+
* The cumulative number of output tokens which were used.
196+
*/
197+
output_tokens: number;
198+
}
199+
175200
export interface MessageParam {
176201
content: string | Array<TextBlock>;
177202

@@ -208,6 +233,18 @@ export interface TextDelta {
208233
type: 'text_delta';
209234
}
210235

236+
export interface Usage {
237+
/**
238+
* The number of input tokens which were used.
239+
*/
240+
input_tokens: number;
241+
242+
/**
243+
* The number of output tokens which were used.
244+
*/
245+
output_tokens: number;
246+
}
247+
211248
export type MessageCreateParams = MessageCreateParamsNonStreaming | MessageCreateParamsStreaming;
212249

213250
export interface MessageCreateParamsBase {
@@ -283,6 +320,11 @@ export interface MessageCreateParamsBase {
283320
* See our
284321
* [guide to prompt design](https://docs.anthropic.com/claude/docs/introduction-to-prompt-design)
285322
* for more details on how to best construct prompts.
323+
*
324+
* Note that if you want to include a
325+
* [system prompt](https://docs.anthropic.com/claude/docs/how-to-use-system-prompts),
326+
* you can use the top-level `system` parameter — there is no `"system"` role for
327+
* input messages in the Messages API.
286328
*/
287329
messages: Array<MessageParam>;
288330

@@ -321,8 +363,8 @@ export interface MessageCreateParamsBase {
321363
/**
322364
* Whether to incrementally stream the response using server-sent events.
323365
*
324-
* See [streaming](https://docs.anthropic.com/claude/reference/streaming) for
325-
* details.
366+
* See [streaming](https://docs.anthropic.com/claude/reference/messages-streaming)
367+
* for details.
326368
*/
327369
stream?: boolean;
328370

@@ -374,7 +416,7 @@ export namespace MessageCreateParams {
374416
* this id to help detect abuse. Do not include any identifying information such as
375417
* name, email address, or phone number.
376418
*/
377-
user_id?: string;
419+
user_id?: string | null;
378420
}
379421

380422
export type MessageCreateParamsNonStreaming = MessagesAPI.MessageCreateParamsNonStreaming;
@@ -385,8 +427,8 @@ export interface MessageCreateParamsNonStreaming extends MessageCreateParamsBase
385427
/**
386428
* Whether to incrementally stream the response using server-sent events.
387429
*
388-
* See [streaming](https://docs.anthropic.com/claude/reference/streaming) for
389-
* details.
430+
* See [streaming](https://docs.anthropic.com/claude/reference/messages-streaming)
431+
* for details.
390432
*/
391433
stream?: false;
392434
}
@@ -395,8 +437,8 @@ export interface MessageCreateParamsStreaming extends MessageCreateParamsBase {
395437
/**
396438
* Whether to incrementally stream the response using server-sent events.
397439
*
398-
* See [streaming](https://docs.anthropic.com/claude/reference/streaming) for
399-
* details.
440+
* See [streaming](https://docs.anthropic.com/claude/reference/messages-streaming)
441+
* for details.
400442
*/
401443
stream: true;
402444
}
@@ -474,6 +516,11 @@ export interface MessageStreamParams {
474516
* See our
475517
* [guide to prompt design](https://docs.anthropic.com/claude/docs/introduction-to-prompt-design)
476518
* for more details on how to best construct prompts.
519+
*
520+
* Note that if you want to include a
521+
* [system prompt](https://docs.anthropic.com/claude/docs/how-to-use-system-prompts),
522+
* you can use the top-level `system` parameter — there is no `"system"` role for
523+
* input messages in the Messages API.
477524
*/
478525
messages: Array<MessageParam>;
479526

@@ -557,7 +604,7 @@ export namespace MessageStreamParams {
557604
* this id to help detect abuse. Do not include any identifying information such as
558605
* name, email address, or phone number.
559606
*/
560-
user_id?: string;
607+
user_id?: string | null;
561608
}
562609
}
563610

@@ -568,12 +615,14 @@ export namespace Messages {
568615
export import ContentBlockStopEvent = MessagesAPI.ContentBlockStopEvent;
569616
export import Message = MessagesAPI.Message;
570617
export import MessageDeltaEvent = MessagesAPI.MessageDeltaEvent;
618+
export import MessageDeltaUsage = MessagesAPI.MessageDeltaUsage;
571619
export import MessageParam = MessagesAPI.MessageParam;
572620
export import MessageStartEvent = MessagesAPI.MessageStartEvent;
573621
export import MessageStopEvent = MessagesAPI.MessageStopEvent;
574622
export import MessageStreamEvent = MessagesAPI.MessageStreamEvent;
575623
export import TextBlock = MessagesAPI.TextBlock;
576624
export import TextDelta = MessagesAPI.TextDelta;
625+
export import Usage = MessagesAPI.Usage;
577626
export import MessageCreateParams = MessagesAPI.MessageCreateParams;
578627
export import MessageCreateParamsNonStreaming = MessagesAPI.MessageCreateParamsNonStreaming;
579628
export import MessageCreateParamsStreaming = MessagesAPI.MessageCreateParamsStreaming;

src/resources/completions.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Stream } from '@anthropic-ai/sdk/streaming';
88

99
export class Completions extends APIResource {
1010
/**
11-
* Create a Completion
11+
* Create a Text Completion
1212
*/
1313
create(body: CompletionCreateParamsNonStreaming, options?: Core.RequestOptions): APIPromise<Completion>;
1414
create(
@@ -59,7 +59,7 @@ export interface Completion {
5959
* `stop_sequences` parameter, or a stop sequence built into the model
6060
* - `"max_tokens"`: we exceeded `max_tokens_to_sample` or the model's maximum
6161
*/
62-
stop_reason: string;
62+
stop_reason: string | null;
6363

6464
type: 'completion';
6565
}
@@ -124,8 +124,9 @@ export interface CompletionCreateParamsBase {
124124
/**
125125
* Whether to incrementally stream the response using server-sent events.
126126
*
127-
* See [streaming](https://docs.anthropic.com/claude/reference/streaming) for
128-
* details.
127+
* See
128+
* [streaming](https://docs.anthropic.com/claude/reference/text-completions-streaming)
129+
* for details.
129130
*/
130131
stream?: boolean;
131132

@@ -168,7 +169,7 @@ export namespace CompletionCreateParams {
168169
* this id to help detect abuse. Do not include any identifying information such as
169170
* name, email address, or phone number.
170171
*/
171-
user_id?: string;
172+
user_id?: string | null;
172173
}
173174

174175
export type CompletionCreateParamsNonStreaming = CompletionsAPI.CompletionCreateParamsNonStreaming;
@@ -179,8 +180,9 @@ export interface CompletionCreateParamsNonStreaming extends CompletionCreatePara
179180
/**
180181
* Whether to incrementally stream the response using server-sent events.
181182
*
182-
* See [streaming](https://docs.anthropic.com/claude/reference/streaming) for
183-
* details.
183+
* See
184+
* [streaming](https://docs.anthropic.com/claude/reference/text-completions-streaming)
185+
* for details.
184186
*/
185187
stream?: false;
186188
}
@@ -189,8 +191,9 @@ export interface CompletionCreateParamsStreaming extends CompletionCreateParamsB
189191
/**
190192
* Whether to incrementally stream the response using server-sent events.
191193
*
192-
* See [streaming](https://docs.anthropic.com/claude/reference/streaming) for
193-
* details.
194+
* See
195+
* [streaming](https://docs.anthropic.com/claude/reference/text-completions-streaming)
196+
* for details.
194197
*/
195198
stream: true;
196199
}

0 commit comments

Comments
 (0)