AWS Bedrock
AWS Bedrock offers two surfaces: direct model invocation (InvokeModel) and Bedrock Agents (managed agent-with-tools runtime with action groups + Knowledge Bases). Trefur ships two adapters — patchBedrock for the model runtime and patchBedrockAgents for the agent runtime — plus the read-only AWS connector for fleet-level inventory.
Install
npm install @trefur/observe @aws-sdk/client-bedrock-agent-runtime @aws-sdk/client-bedrock-runtimeSetup
import * as bedrockRuntime from '@aws-sdk/client-bedrock-runtime';
import * as bedrockAgentRuntime from '@aws-sdk/client-bedrock-agent-runtime';
import {
BedrockAgentRuntimeClient,
InvokeAgentCommand,
} from '@aws-sdk/client-bedrock-agent-runtime';
import {
BedrockRuntimeClient,
InvokeModelCommand,
} from '@aws-sdk/client-bedrock-runtime';
import {
TrefurObserve,
patchBedrock,
patchBedrockAgents,
} from '@trefur/observe';
// Initialise observe once at startup.
TrefurObserve.init({
apiKey: process.env.TREFUR_API_KEY!,
agent: { name: 'bedrock-support', framework: 'bedrock' },
});
// Each patch takes the imported AWS SDK module namespace and the observe
// instance. Call both before you construct your Bedrock clients.
patchBedrock(bedrockRuntime, TrefurObserve.getInstance());
patchBedrockAgents(bedrockAgentRuntime, TrefurObserve.getInstance());
// Direct model invocation (Anthropic Claude on Bedrock)
const modelClient = new BedrockRuntimeClient({ region: 'us-east-1' });
const modelResp = await modelClient.send(new InvokeModelCommand({
modelId: 'anthropic.claude-sonnet-4-20250514-v1:0',
contentType: 'application/json',
body: JSON.stringify({
anthropic_version: 'bedrock-2023-05-31',
max_tokens: 1024,
messages: [{ role: 'user', content: 'Summarise the latest 10-K' }],
}),
}));
// Bedrock Agent invocation
const agentClient = new BedrockAgentRuntimeClient({ region: 'us-east-1' });
const agentResp = await agentClient.send(new InvokeAgentCommand({
agentId: 'AGENT_ID',
agentAliasId: 'TSTALIASID',
sessionId: 'session-' + crypto.randomUUID(),
inputText: 'What is the status of order #12345?',
}));What gets traced
- InvokeModel + InvokeModelWithResponseStream — model id, region, prompt, completion, token counts.
- InvokeAgent — session id, agent id, agent alias, input text, trace events.
- Action group calls — every Lambda invocation triggered by the agent renders as a tool step with input + output.
- Knowledge Base retrievals — the citations object is captured per agent turn.
- Guardrails — applied
guardrailIdentifier+ violations are tagged. - Streaming — chunks are reassembled before emit.
Common use case — Bedrock Agent + Lambda action group
The canonical Bedrock Agents pattern wires an agent to one or more Lambda action groups for tool calls, plus a Knowledge Base for RAG. Trefur renders the full trace: input → agent reasoning → KB retrieval(s) → action-group Lambda call(s) → final answer, with latency per hop. Pair with the AWS connector for IAM-level inventory of every Bedrock surface in the account.
Common pitfalls
- Action-group Lambda telemetry. The patch sees the
InvokeAgenttrace events (which include the Lambda call). If you also want telemetry inside the Lambda, add the Python or Node SDK to the Lambda runtime and wire it to the same Trefur project. - Cross-region. Each region has its own Bedrock endpoint. Calls to multiple regions emit under the same project; the region field on each step lets you filter.
- Python users. The Python adapter is
from trefur_observe import patch_bedrock, patch_bedrock_agents— patches the boto3 client.
See the JavaScript SDK or Python SDK reference, and the AWS connector for fleet-level inventory.