Skip to content

fix(core): set 1.201s default HTTP agent timeout, disable keep-alive for AI Core requests - #2266

Open
InjunPark-sap wants to merge 5 commits into
mainfrom
feat/default-agent-timeout-623
Open

fix(core): set 1.201s default HTTP agent timeout, disable keep-alive for AI Core requests#2266
InjunPark-sap wants to merge 5 commits into
mainfrom
feat/default-agent-timeout-623

Conversation

@InjunPark-sap

Copy link
Copy Markdown
Member

Context

Closes SAP/ai-sdk-js-backlog#623.

What this PR does and why it is needed

The SDK inherited the SAP Cloud SDK's 5s HTTP socket timeout, which is too short for long-running chat and streaming completions. This PR sets a 12-minute (720000 ms) default socket timeout on the service-binding-derived AI Core destination (covering both client-secret and mTLS auth).

Keep-alive is disabled on these requests: with the old 5s timeout, idle pooled sockets were evicted before any load balancer closed them; the longer timeout would otherwise keep stale sockets around long enough to be reused and trigger ECONNRESET. Disabling pooling avoids stale-socket reuse while the long timeout still covers slow responses. Rationale and the researched load-balancer idle timeouts are in the decision record.

Both defaults apply only to the SDK-managed destination and can be overridden via a custom destination's agentOptions or per-request CustomRequestConfig.httpsAgent.

Open for reviewers: confirm disabling keep-alive is acceptable vs. adding agentkeepalive to preserve pooling (would need SAP BTP's actual LB idle timeout to tune). Docs handled in a separate SAP/ai-sdk PR — link to follow.

Definition of Done

  • Code is tested (Unit, E2E)
  • Error handling created / updated & covered by the tests above
  • Documentation updated
    • Only Public APIs are allowed to be used in documentation/tutorials/sample code
  • (Optional) Aligned changes with the Java SDK
  • (Optional) Release notes updated

@InjunPark-sap InjunPark-sap self-assigned this Sep 7, 2026
@InjunPark-sap
InjunPark-sap force-pushed the feat/default-agent-timeout-623 branch from 12c0cbe to 438b5fc Compare September 7, 2026 15:48
@InjunPark-sap
InjunPark-sap marked this pull request as ready for review September 7, 2026 15:55
@hyperspace-pr-bot

Copy link
Copy Markdown
Contributor

Summary

The following content is AI-generated and provides a summary of the pull request:


Context

Closes SAP/ai-sdk-js-backlog#623.

What this PR does and why it is needed

Sets a 12-minute (720,000 ms) default HTTP socket timeout for AI Core requests, overriding the inherited SAP Cloud SDK default of 5 seconds, which is too short for long-running chat and streaming completions.

Keep-alive is also disabled on SDK-managed destinations. With the previous 5s timeout, stale pooled sockets were evicted quickly; the longer timeout would otherwise keep those sockets alive past the load balancer's idle timeout, causing ECONNRESET errors on reuse. Disabling connection pooling avoids this class of stale-socket failures.

Changes

  • packages/core/src/context.ts: Introduces two constants (DEFAULT_AGENT_TIMEOUT = 720_000 and DEFAULT_AGENT_KEEP_ALIVE = false) and merges them into the agentOptions of the service-binding-derived destination. Pre-existing agentOptions on the destination take precedence (spread order ensures user values win).
  • packages/core/src/context.test.ts: Adds test coverage for:
    • Default timeout injection for client-secret and mTLS-shaped destinations
    • No injection when a user-provided destination is passed directly
    • Pre-existing agentOptions are not overwritten
  • .changeset/default-agent-timeout.md: Patch-level changelog entry for @sap-ai-sdk/core.

ℹ️ Both defaults apply only to the SDK-managed service-binding destination and can be overridden via a custom destination's agentOptions or per-request CustomRequestConfig.httpsAgent.

Category: 🐛 Bug Fix


  • 🔄 Regenerate and Update Summary
  • ✏️ Insert as PR Description (deletes this comment)
  • 🗑️ Delete comment
PR Bot Information

Version: 1.31.14

Comment thread packages/core/src/context.ts Outdated
Comment on lines +25 to +27
// Disabled so the long timeout above does not keep stale pooled sockets alive past
// a load balancer's idle timeout, which would cause ECONNRESET on reuse.
const DEFAULT_AGENT_KEEP_ALIVE = false;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer enabling this by default. Please investigate if increasing the timeout actually leads to issues and if the keep alive timeout can be configured in isolation.

As far as I know, this is also the default.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Node core can't isolate idle vs active timeout — one timeout governs both (keepAliveMsecs is only the TCP probe delay). Isolation needs agentkeepalive(new dep).

  2. 12min + keepAlive:true has a documented risk (ECONNRESET while doing http 1.1 keep alive requests and server closes the connections nodejs/node#47130): if the LB drops idle conns first, the reused half-open socket → ECONNRESET, no core fix. The old 5s timeout avoided this implicitly.

So it hinges on the AI Core LB idle timeout. If it is more than 12min, keepAlive:true + 1200s is safe and I'll switch; if shorter, we add agentkeepalive or keep keepAlive:false. WDYT?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, sadly axios does not appear to handle this, please add a comment here to re-visit this if we do switch to a different HTTP library like unidici.
The fetch adapter would be an option for non-mTLS connections, but that could also lead to issues with any custom HTTP middleware users might have.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Added a comment to revisit keepAlive if we switch HTTP library (undici / fetch adapter).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the TODO: string to make it easier to find.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — prefixed the revisit comment with TODO:.

}
)) as HttpDestination;
return aiCoreDestination;
return {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[req] Ensure the other axios options mentioned in the documentation are added; I think we might already set timeout: 0 somewhere.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

timeout: 0 is the axios request-level timeout in Cloud SDK default — no request deadline, deliberate for streaming.

Separate layer from the agent socket timeouthere, no conflict. CustomRequestConfig already exposes httpsAgent/httpAgent/proxy/signal/middleware; axios timeout isn't picked.

Do you want it to be added, or other options?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added maxContentLength/maxBodyLength: Infinity to the default request config.

Note it's a no-op on the current http adapter (axios defaults both to -1 = unlimited), but makes the intent explicit and future-proofs a switch to undici/fetch.

timeout: 0 is already set by the Cloud SDK (https://github.com/SAP/cloud-sdk-js/blob/052efd32b668687051e9f0f91883a241ecd6a5f4/packages/http-client/src/http-client.ts#L494).

Co-authored-by: David Knaack <david.knaack@sap.com>
@davidkna-sap davidkna-sap changed the title fix(core): set 12min default HTTP agent timeout, disable keep-alive for AI Core requests fix(core): set 1.201s default HTTP agent timeout, disable keep-alive for AI Core requests Sep 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants