feat: add directSend feature for low-latency token streaming #700
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR introduces the
directSendfeature that enables Lambda handlers to send LLM tokens directly to clients via AppSync GraphQL mutations, bypassing the SNS/SQS messaging path for significantly reduced latency during streaming responses.Motivation
The default token delivery path (Lambda → SNS → SQS → WebSocket) introduces unnecessary latency for streaming LLM responses. By sending tokens directly via AppSync, we achieve:
Changes
Core Implementation
directSend?: booleantoSystemConfig(default:false)appsync.pymodule with SigV4-authenticated GraphQL mutationswebsocket.pyto routeLLM_NEW_TOKENactions based onDIRECT_SENDenv varFiles Changed (11)
cli/magic-config.ts- Configuration wizard supportlib/shared/types.ts- Type definitionlib/shared/layers/python-sdk/python/genai_core/utils/appsync.py- NEW: Direct send implementationlib/shared/layers/python-sdk/python/genai_core/utils/websocket.py- Routing logiclib/model-interfaces/{langchain,idefics,bedrock-agents}/index.ts- Interface updateslib/aws-genai-llm-chatbot-stack.ts- Pass GraphQL API to interfacestests/shared/test_*.py- Unit testsTesting
Backward Compatibility
directSend: false)Deployment Notes
When enabled, Lambda functions require:
APPSYNC_ENDPOINTenvironment variableDIRECT_SEND=trueenvironment variableappsync:GraphQL(query/mutation)Checklist