This tutorial demonstrates how to run a Strands Agent entirely in the browser using the TypeScript SDK with Vite as the build tool. The example creates a simple chat interface where users can interact with an agent directly from their web browser.
| Feature | Description |
|---|---|
| Agent Structure | Single agent architecture |
| Architecture | Client-side only (browser) |
| Build Tool | Vite |
| Model Providers | Amazon Bedrock (Claude Sonnet 4), OpenAI (GPT-4o) |
- Node.js 18.x or later
- One of the following:
- AWS credentials with Amazon Bedrock access (Access Key ID, Secret Access Key, and optionally Session Token for temporary credentials)
- OpenAI API key
- Basic TypeScript and web development knowledge
03-browser-agent/
├── images/
│ └── browser_agent.png # Architecture diagram
├── index.html # Entry HTML with chat UI
├── src/
│ ├── main.ts # Agent setup and DOM interaction
│ ├── style.css # Chat interface styling
│ └── vite-env.d.ts # Vite type declarations
├── package.json # Dependencies
├── tsconfig.json # TypeScript configuration
├── vite.config.ts # Vite configuration
└── README.md # This file
cd typescript/01-learn/06-browser-agent
npm install
npm run devThis will start the Vite dev server and open http://localhost:5173 in your browser.
The agent runs entirely in the browser, which means:
- No backend required - API calls go directly from the browser to the model provider (development only - see Security Considerations)
- Simple deployment - Can be hosted on any static file server (do not bundle credentials)
In browser environments, credentials must be passed explicitly. The application supports two model providers:
import { Agent, BedrockModel } from "@strands-agents/sdk";
const agent = new Agent({
model: new BedrockModel({
region: "us-east-1",
clientConfig: {
credentials: {
accessKeyId: "...",
secretAccessKey: "...",
sessionToken: "...", // Optional, for temporary credentials
},
},
}),
systemPrompt: "You are a helpful assistant running in the browser."
});import { Agent } from "@strands-agents/sdk";
import { OpenAIModel } from "@strands-agents/sdk/openai";
const agent = new Agent({
model: new OpenAIModel({
apiKey: "sk-...",
modelId: "gpt-4o",
// Required to allow OpenAI SDK to run in browser environments
clientConfig: {
dangerouslyAllowBrowser: true,
},
}),
systemPrompt: "You are a helpful assistant running in the browser."
});Streaming displays the response progressively as it's generated, providing immediate feedback:
for await (const event of agent.stream(userMessage)) {
if (
event.type === "modelContentBlockDeltaEvent" &&
event.delta.type === "textDelta"
) {
responseText += event.delta.text;
// Update UI with each chunk
}
}Vite is a build tool that enables browser-based development:
- TypeScript compilation - Compiles
.tsfiles to JavaScript since browsers can't run TypeScript directly - Module bundling - Resolves and bundles dependencies (like
@strands-agents/sdk) so browsers can load them - Dev server - Serves files locally with automatic reload on save
- Production builds - Creates optimized, minified files for deployment
When running agents in the browser, be aware of:
- Credentials Exposure - AWS credentials and OpenAI API keys used in browser code are visible in the network tab and browser developer tools
- For Production - Use a backend proxy or secure token-based authentication
- Development Only - This tutorial is intended for development and demonstration purposes
npm run buildThis creates an optimized build in the dist/ directory that can be deployed to any static hosting service.


