Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,8 @@ jobs:
- name: Bootstrap
run: ./scripts/bootstrap

- name: Update 3p souces
run: ./bin/replace-internal-symlinks

- name: Check build
run: yarn build
run: ./scripts/build

- name: Get GitHub OIDC Token
if: github.repository == 'stainless-sdks/anthropic-typescript'
Expand Down
46 changes: 0 additions & 46 deletions .github/workflows/create-releases.yml

This file was deleted.

21 changes: 10 additions & 11 deletions .github/workflows/publish-npm.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# workflow for re-running publishing to NPM in case it fails for some reason
# you can run this workflow by navigating to https://www.github.com/anthropics/anthropic-sdk-typescript/actions/workflows/publish-npm.yml
# This workflow is triggered when a GitHub release is created.
# It can also be run manually to re-publish to NPM in case it failed for some reason.
# You can run this workflow by navigating to https://www.github.com/anthropics/anthropic-sdk-typescript/actions/workflows/publish-npm.yml
name: Publish NPM
on:
workflow_dispatch:
inputs:
path:
description: The path to run the release in, e.g. '.' or 'packages/vertex-sdk'
required: true

release:
types: [published]

jobs:
publish:
Expand All @@ -18,19 +18,18 @@ jobs:
- uses: actions/checkout@v4

- name: Set up Node
uses: actions/setup-node@v4
uses: actions/setup-node@v3
with:
node-version: '20'

- name: Install dependencies
run: |
yarn install

- name: Update 3p souces
run: ./bin/replace-internal-symlinks

- name: Publish to NPM
run: |
yarn tsn scripts/publish-packages.ts '{ "paths_released": "[\"${{ github.event.inputs.path }}\"]" }'
yarn tsn scripts/publish-packages.ts

env:
DATA: ${{ toJSON(steps.release.outputs) }}
NPM_TOKEN: ${{ secrets.ANTHROPIC_NPM_TOKEN || secrets.NPM_TOKEN }}
1 change: 0 additions & 1 deletion .github/workflows/release-doctor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@ jobs:
run: |
bash ./bin/check-release-environment
env:
STAINLESS_API_KEY: ${{ secrets.STAINLESS_API_KEY }}
NPM_TOKEN: ${{ secrets.ANTHROPIC_NPM_TOKEN || secrets.NPM_TOKEN }}
1 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ CHANGELOG.md

# don't format tsc output, will break source maps
/dist
/packages/*/dist
4 changes: 1 addition & 3 deletions .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{
".": "0.50.1",
"packages/vertex-sdk": "0.11.0",
"packages/bedrock-sdk": "0.21.0"
".": "0.1.0-alpha.1"
}
2 changes: 1 addition & 1 deletion .stats.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
configured_endpoints: 21
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/anthropic%2Fanthropic-7015ea2d98991d6c2e7931c521e36448778fe868cc1b8a21173898d67b14b819.yml
openapi_spec_hash: 2007ff815a3f39af8cebe1976d50f17d
config_hash: 4d0dcf47d77eae22d34624d2ac0f0b46
config_hash: 55aeaebf89fb2ccfcd0b188fc4dc6aba
1,830 changes: 270 additions & 1,560 deletions CHANGELOG.md

Large diffs are not rendered by default.

174 changes: 15 additions & 159 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
> [!IMPORTANT]
> We're actively working on a new alpha version that migrates from `node-fetch` to builtin fetch.
>
> Please try it out and let us know if you run into any issues!
> https://github.com/anthropics/anthropic-sdk-typescript/issues/645

# Anthropic TypeScript API Library

[![NPM version](https://img.shields.io/npm/v/@anthropic-ai/sdk.svg)](https://npmjs.org/package/@anthropic-ai/sdk) ![npm bundle size](https://img.shields.io/bundlephobia/minzip/@anthropic-ai/sdk)
Expand Down Expand Up @@ -92,104 +86,6 @@ main();

Documentation for each method, request param, and response field are available in docstrings and will appear on hover in most modern editors.

## Counting Tokens

You can see the exact usage for a given request through the `usage` response property, e.g.

```ts
const message = await client.messages.create(...)
console.log(message.usage)
// { input_tokens: 25, output_tokens: 13 }
```

## Streaming Helpers

This library provides several conveniences for streaming messages, for example:

```ts
import Anthropic from '@anthropic-ai/sdk';

const anthropic = new Anthropic();

async function main() {
const stream = anthropic.messages
.stream({
model: 'claude-3-5-sonnet-latest',
max_tokens: 1024,
messages: [
{
role: 'user',
content: 'Say hello there!',
},
],
})
.on('text', (text) => {
console.log(text);
});

const message = await stream.finalMessage();
console.log(message);
}

main();
```

Streaming with `client.messages.stream(...)` exposes [various helpers for your convenience](helpers.md) including event handlers and accumulation.

Alternatively, you can use `client.messages.create({ ..., stream: true })` which only returns an async iterable of the events in the stream and thus uses less memory (it does not build up a final message object for you).

## Message Batches

This SDK provides beta support for the [Message Batches API](https://docs.anthropic.com/en/docs/build-with-claude/message-batches) under the `client.beta.messages.batches` namespace.

### Creating a batch

Message Batches takes an array of requests, where each object has a `custom_id` identifier, and the exact same request `params` as the standard Messages API:

```ts
await anthropic.beta.messages.batches.create({
requests: [
{
custom_id: 'my-first-request',
params: {
model: 'claude-3-5-sonnet-latest',
max_tokens: 1024,
messages: [{ role: 'user', content: 'Hello, world' }],
},
},
{
custom_id: 'my-second-request',
params: {
model: 'claude-3-5-sonnet-latest',
max_tokens: 1024,
messages: [{ role: 'user', content: 'Hi again, friend' }],
},
},
],
});
```

### Getting results from a batch

Once a Message Batch has been processed, indicated by `.processing_status === 'ended'`, you can access the results with `.batches.results()`

```ts
const results = await anthropic.beta.messages.batches.results(batch_id);
for await (const entry of results) {
if (entry.result.type === 'succeeded') {
console.log(entry.result.message.content);
}
}
```

## Tool use

This SDK provides support for tool use, aka function calling. More details can be found in [the documentation](https://docs.anthropic.com/claude/docs/tool-use).

## AWS Bedrock

We provide support for the [Anthropic Bedrock API](https://aws.amazon.com/bedrock/claude/) through a [separate package](https://github.com/anthropics/anthropic-sdk-typescript/tree/main/packages/bedrock-sdk).

## Handling errors

When the library is unable to connect to the API,
Expand Down Expand Up @@ -232,21 +128,6 @@ Error codes are as follows:
| >=500 | `InternalServerError` |
| N/A | `APIConnectionError` |

## Request IDs

> For more information on debugging requests, see [these docs](https://docs.anthropic.com/en/api/errors#request-id)

All object responses in the SDK provide a `_request_id` property which is added from the `request-id` response header so that you can quickly log failing requests and report them back to Anthropic.

```ts
const message = await client.messages.create({
max_tokens: 1024,
messages: [{ role: 'user', content: 'Hello, Claude' }],
model: 'claude-3-5-sonnet-latest',
});
console.log(message._request_id); // req_018EeWyXxfu5pfWkrYcMdjWG
```

### Retries

Certain errors will be automatically retried 2 times by default, with a short exponential backoff.
Expand All @@ -270,18 +151,7 @@ await client.messages.create({ max_tokens: 1024, messages: [{ role: 'user', cont

### Timeouts

By default requests time out after 10 minutes. However if you have specified a large `max_tokens` value and are
_not_ streaming, the default timeout will be calculated dynamically using the formula:

```typescript
const minimum = 10 * 60;
const calculated = (60 * 60 * maxTokens) / 128_000;
return calculated < minimum ? minimum * 1000 : calculated * 1000;
```

which will result in a timeout up to 60 minutes, scaled by the `max_tokens` parameter, unless overriden at the request or client level.

You can configure this with a `timeout` option:
Requests time out after 10 minutes by default. You can configure this with a `timeout` option:

<!-- prettier-ignore -->
```ts
Expand All @@ -300,24 +170,6 @@ On timeout, an `APIConnectionTimeoutError` is thrown.

Note that requests which time out will be [retried twice by default](#retries).

### Long Requests

> [!IMPORTANT]
> We highly encourage you use the streaming [Messages API](#streaming-responses) for longer running requests.

We do not recommend setting a large `max_tokens` values without using streaming.
Some networks may drop idle connections after a certain period of time, which
can cause the request to fail or [timeout](#timeouts) without receiving a response from Anthropic.

This SDK will also throw an error if a non-streaming request is expected to be above roughly 10 minutes long.
Passing `stream: true` or [overriding](#timeouts) the `timeout` option at the client or request level disables this error.

An expected request latency longer than the [timeout](#timeouts) for a non-streaming request
will result in the client terminating the connection and retrying without receiving a response.

When supported by the `fetch` implementation, we set a [TCP socket keep-alive](https://tldp.org/HOWTO/TCP-Keepalive-HOWTO/overview.html)
option in order to reduce the impact of idle connection timeouts on some networks.

## Auto-pagination

List methods in the Anthropic API are paginated.
Expand Down Expand Up @@ -610,19 +462,23 @@ The following runtimes are supported:
- Vercel Edge Runtime.
- Jest 28 or greater with the `"node"` environment (`"jsdom"` is not supported at this time).
- Nitro v2.6 or greater.
- Web browsers: disabled by default to avoid exposing your secret API credentials (see our help center for [best practices](https://support.anthropic.com/en/articles/9767949-api-key-best-practices-keeping-your-keys-safe-and-secure)). Enable browser support by explicitly setting `dangerouslyAllowBrowser` to `true`.
- Web browsers: disabled by default to avoid exposing your secret API credentials. Enable browser support by explicitly setting `dangerouslyAllowBrowser` to true'.
<details>
<summary>More explanation</summary>

### Why is this dangerous?

<details>
<summary><b>More explanation</b></summary>
<h3>Why is this dangerous?</h3>
Enabling the <code>dangerouslyAllowBrowser</code> option can be dangerous because it exposes your secret API credentials in the client-side code. Web browsers are inherently less secure than server environments,
Enabling the `dangerouslyAllowBrowser` option can be dangerous because it exposes your secret API credentials in the client-side code. Web browsers are inherently less secure than server environments,
any user with access to the browser can potentially inspect, extract, and misuse these credentials. This could lead to unauthorized access using your credentials and potentially compromise sensitive data or functionality.
<h3>When might this not be dangerous?</h3>

### When might this not be dangerous?

In certain scenarios where enabling browser support might not pose significant risks:
<ul>
<li>Internal Tools: If the application is used solely within a controlled internal environment where the users are trusted, the risk of credential exposure can be mitigated.</li>
<li>Development or debugging purpose: Enabling this feature temporarily might be acceptable, provided the credentials are short-lived, aren't also used in production environments, or are frequently rotated.</li>
</ul>

- Internal Tools: If the application is used solely within a controlled internal environment where the users are trusted, the risk of credential exposure can be mitigated.
- Public APIs with Limited Scope: If your API has very limited scope and the exposed credentials do not grant access to sensitive data or critical operations, the potential impact of exposure is reduced.
- Development or debugging purpose: Enabling this feature temporarily might be acceptable, provided the credentials are short-lived, aren't also used in production environments, or are frequently rotated.

</details>

Note that React Native is not supported at this time.
Expand Down
13 changes: 0 additions & 13 deletions api.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Anthropic

# Shared

Types:
Expand Down Expand Up @@ -37,14 +35,11 @@ Types:
- <code><a href="./src/resources/messages/messages.ts">ContentBlockParam</a></code>
- <code><a href="./src/resources/messages/messages.ts">ContentBlockSource</a></code>
- <code><a href="./src/resources/messages/messages.ts">ContentBlockSourceContent</a></code>
- <code><a href="./src/resources/messages/messages.ts">ContentBlockStartEvent</a></code>
- <code><a href="./src/resources/messages/messages.ts">ContentBlockStopEvent</a></code>
- <code><a href="./src/resources/messages/messages.ts">DocumentBlockParam</a></code>
- <code><a href="./src/resources/messages/messages.ts">ImageBlockParam</a></code>
- <code><a href="./src/resources/messages/messages.ts">InputJSONDelta</a></code>
- <code><a href="./src/resources/messages/messages.ts">Message</a></code>
- <code><a href="./src/resources/messages/messages.ts">MessageCountTokensTool</a></code>
- <code><a href="./src/resources/messages/messages.ts">MessageDeltaEvent</a></code>
- <code><a href="./src/resources/messages/messages.ts">MessageDeltaUsage</a></code>
- <code><a href="./src/resources/messages/messages.ts">MessageParam</a></code>
- <code><a href="./src/resources/messages/messages.ts">MessageTokensCount</a></code>
Expand Down Expand Up @@ -101,19 +96,11 @@ Types:
- <code><a href="./src/resources/messages/messages.ts">WebSearchToolResultBlockParam</a></code>
- <code><a href="./src/resources/messages/messages.ts">WebSearchToolResultBlockParamContent</a></code>
- <code><a href="./src/resources/messages/messages.ts">WebSearchToolResultError</a></code>
- <code><a href="./src/resources/messages/messages.ts">MessageStreamEvent</a></code>
- <code><a href="./src/resources/messages/messages.ts">MessageStartEvent</a></code>
- <code><a href="./src/resources/messages/messages.ts">MessageDeltaEvent</a></code>
- <code><a href="./src/resources/messages/messages.ts">MessageStopEvent</a></code>
- <code><a href="./src/resources/messages/messages.ts">ContentBlockStartEvent</a></code>
- <code><a href="./src/resources/messages/messages.ts">ContentBlockDeltaEvent</a></code>
- <code><a href="./src/resources/messages/messages.ts">ContentBlockStopEvent</a></code>

Methods:

- <code title="post /v1/messages">client.messages.<a href="./src/resources/messages/messages.ts">create</a>({ ...params }) -> Message</code>
- <code title="post /v1/messages/count_tokens">client.messages.<a href="./src/resources/messages/messages.ts">countTokens</a>({ ...params }) -> MessageTokensCount</code>
- <code>client.messages.<a href="./src/resources/messages.ts">stream</a>(body, options?) -> MessageStream</code>

## Batches

Expand Down
Loading