Skip to content

InvokeAgent

Source: src/AWS/Bedrock/InvokeAgent.ts

Runtime binding for bedrock-agent-runtime:InvokeAgent — send user input to a Bedrock agent through one of its aliases and receive the agent’s response as an event stream.

Bind an AgentAlias inside a function runtime to get a callable that invokes the agent. The binding grants the function bedrock:InvokeAgent scoped to exactly that alias. The response’s completion is an event Stream of chunks (and traces when enableTrace is set); concatenate the chunk bytes to recover the answer.

Invoke and Aggregate the Completion

// init
const invokeAgent = yield* Bedrock.InvokeAgent(alias);
// runtime
const result = yield* invokeAgent({
sessionId: crypto.randomUUID(),
inputText: "What is the capital of France?",
});
const events = yield* Stream.runCollect(result.completion);
const decoder = new TextDecoder();
const answer = events
.map((event) =>
event.chunk?.bytes !== undefined
? decoder.decode(
Redacted.isRedacted(event.chunk.bytes)
? Redacted.value(event.chunk.bytes)
: event.chunk.bytes,
)
: "",
)
.join("");

Continue a Session

// Reuse the same sessionId across calls to keep conversational context.
const followUp = yield* invokeAgent({
sessionId,
inputText: "And its population?",
});