File tree Expand file tree Collapse file tree 2 files changed +57
-0
lines changed Expand file tree Collapse file tree 2 files changed +57
-0
lines changed Original file line number Diff line number Diff line change
1
+ import Anthropic from '@anthropic-ai/sdk' ;
2
+
3
+ const client = new Anthropic ( ) ; // gets API Key from environment variable ANTHROPIC_API_KEY
4
+
5
+ async function main ( ) {
6
+ let thinkingState = 'not-started' ;
7
+
8
+ const stream = client . messages
9
+ . stream ( {
10
+ model : 'claude-3-7-sonnet-20250219' ,
11
+ max_tokens : 3200 ,
12
+ thinking : { type : 'enabled' , budget_tokens : 1600 } ,
13
+ messages : [ { role : 'user' , content : 'Create a haiku about Anthropic.' } ] ,
14
+ } )
15
+ . on ( 'thinking' , ( thinking ) => {
16
+ if ( thinkingState === 'not-started' ) {
17
+ console . log ( 'Thinking:\n---------' ) ;
18
+ thinkingState = 'started' ;
19
+ }
20
+
21
+ process . stdout . write ( thinking ) ;
22
+ } )
23
+ . on ( 'text' , ( text ) => {
24
+ if ( thinkingState !== 'finished' ) {
25
+ console . log ( '\n\nText:\n-----' ) ;
26
+ thinkingState = 'finished' ;
27
+ }
28
+ process . stdout . write ( text ) ;
29
+ } ) ;
30
+
31
+ const finalMessage = await stream . finalMessage ( ) ;
32
+ console . log ( '\n\nFinal message object:\n--------------------' , finalMessage ) ;
33
+ }
34
+
35
+ main ( ) ;
Original file line number Diff line number Diff line change
1
+ import Anthropic from '@anthropic-ai/sdk' ;
2
+
3
+ const client = new Anthropic ( ) ;
4
+
5
+ async function main ( ) {
6
+ const message = await client . messages . create ( {
7
+ model : 'claude-3-7-sonnet-20250219' ,
8
+ max_tokens : 3200 ,
9
+ thinking : { type : 'enabled' , budget_tokens : 1600 } ,
10
+ messages : [ { role : 'user' , content : 'Create a haiku about Anthropic.' } ] ,
11
+ } ) ;
12
+
13
+ for ( const block of message . content ) {
14
+ if ( block . type === 'thinking' ) {
15
+ console . log ( `Thinking: ${ block . thinking } ` ) ;
16
+ } else if ( block . type === 'text' ) {
17
+ console . log ( `Text: ${ block . text } ` ) ;
18
+ }
19
+ }
20
+ }
21
+
22
+ main ( ) ;
You can’t perform that action at this time.
0 commit comments