Skip to content

Commit db6f761

Browse files
committed
docs: add thinking examples
1 parent 142f221 commit db6f761

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

examples/thinking-stream.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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();

examples/thinking.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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();

0 commit comments

Comments
 (0)