Skip to content

Commit

Permalink
feat (docs): add anthropic 8192 token example (#2751)
Browse files Browse the repository at this point in the history
  • Loading branch information
lgrammel authored Aug 21, 2024
1 parent 46df27c commit f278616
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
24 changes: 24 additions & 0 deletions content/providers/01-ai-sdk-providers/05-anthropic.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,30 @@ console.log(result.experimental_providerMetadata?.anthropic);
// e.g. { cacheCreationInputTokens: 2118, cacheReadInputTokens: 0 }
```

### Example: 8192 Max Tokens

The Anthropic beta `max-tokens-3-5-sonnet-2024-07-15` allows you to generate up to 8192 tokens.
You can use it as follows:

```ts highlight="6-8"
import { anthropic } from '@ai-sdk/anthropic';
import { streamText } from 'ai';

const result = await generateText({
model: anthropic('claude-3-5-sonnet-20240620'),
// enable max tokens beta:
headers: { 'anthropic-beta': 'max-tokens-3-5-sonnet-2024-07-15' },
maxTokens: 8192, // important: specify max tokens
system:
"Don't shy away from generating long text. Follow the user's instructions'",
prompt:
'Invent a new holiday and describe its traditions. Write at least 20 pages.',
});

console.log(result.text);
console.log('Token usage:', result.usage);
```

### Model Capabilities

| Model | Image Input | Object Generation | Tool Usage | Tool Streaming |
Expand Down
29 changes: 29 additions & 0 deletions examples/ai-core/src/stream-text/anthropic-max-tokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { anthropic } from '@ai-sdk/anthropic';
import { streamText } from 'ai';
import dotenv from 'dotenv';

dotenv.config();

async function main() {
const result = await streamText({
model: anthropic('claude-3-5-sonnet-20240620'),

headers: { 'anthropic-beta': 'max-tokens-3-5-sonnet-2024-07-15' },
maxTokens: 8192, // important: specify max tokens

system:
"Don't shy away from generating long text. Follow the user's instructions'",
prompt:
'Invent a new holiday and describe its traditions. Write at least 20 pages.',
});

for await (const textPart of result.textStream) {
process.stdout.write(textPart);
}

console.log();
console.log('Token usage:', await result.usage);
console.log('Finish reason:', await result.finishReason);
}

main().catch(console.error);

0 comments on commit f278616

Please sign in to comment.