Fix TypeError: messages.create(...).withResponse is not a function in Anthropic SDK
Error message
TypeError: messages.create(...).withResponse is not a function
Diagnosis
The error TypeError: messages.create(...).withResponse is not a function occurs when the Anthropic TypeScript SDK's messages.create() method returns a plain JavaScript Promise instead of the expected APIPromise object. The APIPromise class provides the .withResponse() and .asResponse() methods, so when a regular Promise is returned instead, calling either of those methods throws this type error. The most common cause is a version conflict where a different copy of the @anthropic-ai/sdk package is loaded at runtime than the one your code imports, often introduced by a transitive dependency like an OpenTelemetry or APM instrumentation library.
What Causes This Error
1. Conflicting SDK versions from a transitive dependency (most common)
According to the GitHub issue resolution (Source 2), the original reporter traced the problem to an outdated traceloop dependency. The traceloop package pulled in its own copy of @anthropic-ai/sdk, which was a different version than the one the application directly depended on. At runtime, the traceloop package's instrumentation wrapped messages.create() and returned a Promise from its own SDK copy, which lacked the .withResponse() method. This is a classic dependency conflict scenario where npm or yarn hoists or duplicates packages.
2. Duplicate SDK versions in the dependency tree
Even without a specific APM library, having two different versions of @anthropic-ai/sdk in your node_modules can cause this error. The SDK's MessageStream class internally calls messages.create() and expects the result to be an APIPromise. If the runtime resolves to a different SDK instance, the method signature may differ. The GitHub issue maintainer (Source 2) explicitly recommends running npm ls @anthropic-ai/sdk to check for duplicates.
3. Outdated SDK version (less common, but documented)
One affected user in the GitHub thread (Source 2) resolved the issue by upgrading from ^0.29.0 to a current release. While the error is not caused by the SDK version alone in most cases, older versions may have subtle differences in how APIPromise is exported or how MessageStream interacts with it.
4. Environment-specific behavior (not fully diagnosed)
The original reporter (Source 1) could reproduce the error in a Docker container in production but not locally on a MacBook. Another user reported the error only on Heroku. The maintainer (Source 2) notes that the Heroku-only failure was never conclusively diagnosed, though duplicate SDK versions were ruled out in that case. This suggests that build tools, bundlers, or platform-specific module resolution can trigger the conflict.
How to Fix It

Solution 1: Check for duplicate SDK versions with npm ls
This is the first diagnostic step recommended by the SDK maintainer (Source 2). Run the following command in your project root:
npm ls @anthropic-ai/sdk
If you see more than one version listed (e.g., 0.29.0 and 0.32.0), you have a duplicate. The output will show which package depends on which version. For example:
my-app@1.0.0 /path/to/project
├── @anthropic-ai/sdk@0.32.0
└─┬ traceloop@1.2.3
└── @anthropic-ai/sdk@0.29.0
In this case, traceloop brings in its own older SDK. To fix this, you need to either:
- Update the conflicting dependency (
traceloopin this example) to a version that uses the same SDK version as your project. - Use
npm overridesorresolutions(inpackage.json) to force the conflicting dependency to use your SDK version. For npm:
{
"overrides": {
"traceloop": {
"@anthropic-ai/sdk": "^0.32.0"
}
}
}
For Yarn:
{
"resolutions": {
"@anthropic-ai/sdk": "^0.32.0"
}
}
After adding the override, run npm install or yarn install and verify with npm ls @anthropic-ai/sdk again. You should see only one version.
Solution 2: Review OpenTelemetry or APM auto-instrumentation
If you use any OpenTelemetry, APM, or monitoring library that automatically instruments HTTP calls (e.g., traceloop, @opentelemetry/instrumentation-http, dd-trace, newrelic), check whether it wraps messages.create(). The maintainer (Source 2) specifically calls out "any OpenTelemetry/APM auto-instrumentation that might be wrapping messages.create()" as a potential cause.
To test this, temporarily disable the instrumentation library. If the error goes away, you have identified the culprit. You may need to:
- Update the instrumentation library to a version that properly handles the
APIPromisetype. - Configure the instrumentation to exclude Anthropic SDK calls from wrapping.
- Or, as a workaround, avoid using
.withResponse()or.asResponse()and use the rawPromiseinstead (see Solution 4).
Solution 3: Upgrade the Anthropic SDK to the latest version
If you are on an older version like ^0.29.0, upgrade to the current release. The GitHub issue (Source 2) reports that one user resolved the problem this way. Run:
npm install @anthropic-ai/sdk@latest
Or specify a specific recent version:
npm install @anthropic-ai/sdk@0.32.0
After upgrading, test your code. Note that this fix alone may not work if the root cause is a conflicting transitive dependency, but it eliminates the possibility that the SDK itself is the problem.
Solution 4: Workaround: avoid .withResponse() and .asResponse()
If you cannot resolve the dependency conflict immediately, you can work around the error by not calling .withResponse() or .asResponse(). The maintainer (Source 2) notes that one affected user "worked around it by avoiding .asResponse() and is no longer on the project."
Instead of:
const response = await anthropic.messages.create({...}).withResponse();
Use:
const message = await anthropic.messages.create({...});
// message is already the Message object, no need for .withResponse()
If you need the raw HTTP response headers or status code, you can use the asResponse() method only if it's available. But if you are getting this error, it's not available, so you must omit it. The MessageStream class (used in the original reporter's code) also calls .withResponse() internally, so this workaround may not apply if you are using anthropic.messages.stream(). In that case, you must fix the underlying dependency conflict.
If Nothing Works
If none of the above solutions resolve the error, the GitHub maintainer (Source 2) advises:
-
Run
npm ls @anthropic-ai/sdkand verify there is exactly one version. If there is, but the error persists, check for any bundler or build tool that might be resolving modules differently in production vs. development (e.g., Webpack, esbuild, Docker layer caching). -
Review your
node_modulesdirectory structure. In Docker, the build context may include a different set of packages than your local environment. Ensure yourDockerfilerunsnpm ci(clean install) rather thannpm installto guarantee exact dependency resolution. -
Open a new issue on the Anthropic SDK TypeScript GitHub repository (https://github.com/anthropics/anthropic-sdk-typescript/issues/new) with a minimal reproduction. The maintainer (Source 2) explicitly requests: "feel free to open a new issue with a minimal repro if it persists after that." Include:
- The output of
npm ls @anthropic-ai/sdk - Your
package.jsondependencies - A minimal code snippet that reproduces the error
- Whether it happens in production only or also locally
- Any OpenTelemetry or APM libraries you use
- The output of
-
As a temporary workaround, you can use the raw HTTP API with
curlorfetchdirectly, as the original reporter (Source 1) confirmed that acurlcommand worked fine in the production pod:
curl https://api.anthropic.com/v1/messages \
--header "anthropic-version: 2023-06-01" \
--header "content-type: application/json" \
--header "x-api-key: $ANTHROPIC_API_KEY" \
--data \
'{
"model": "claude-3-7-sonnet-20250219",
"messages": [{"role": "user", "content": "Hello"}],
"max_tokens": 256,
"stream": true
}'
This bypasses the SDK entirely and can serve as a stopgap while you debug the dependency issue.
How to Prevent It
-
Pin your SDK version and use lockfiles. Use
npm cioryarn install --frozen-lockfilein CI/CD and Docker builds to ensure the exact same dependency tree is installed every time. This prevents accidental version mismatches between environments. -
Audit transitive dependencies. When adding any OpenTelemetry, APM, or monitoring library that auto-instruments HTTP calls, check its
package.jsonfor@anthropic-ai/sdkin its dependencies. If it lists the SDK, ensure the version range is compatible with yours. Usenpm ls @anthropic-ai/sdkafter installation to confirm no duplicates. -
Use overrides proactively. If you use any instrumentation library that might bundle the Anthropic SDK, add an override in your
package.jsonto force a single version:
{
"overrides": {
"@anthropic-ai/sdk": "^0.32.0"
}
}
-
Test in production-like environments. The original reporter (Source 1) could not reproduce the error locally but saw it in Docker. Run your tests in a Docker container or CI environment that mirrors production to catch dependency conflicts early.
-
Keep the SDK updated. Regularly update
@anthropic-ai/sdkto the latest version. While version alone is rarely the root cause, newer versions may have better error messages or compatibility with common instrumentation libraries.
The #1 Claude Newsletter
The most important claude updates, guides, and fixes — one weekly email.
No spam, unsubscribe anytime. Privacy policy
Related Error Solutions
Keep exploring Claude
Claude resources
Latest AI answers
Skip the manual work
Ready-made AI workflows and automation templates — import and run instead of building from scratch.