1. Install AgentKit

Within an existing project, install AgentKit, Browserbase, and Playwright core:

npm install @inngest/agent-kit @browserbasehq/sdk playwright-core

Don’t have an existing project? Create a new one with your preferred package manager before continuing.

2. Setup an AgentKit Network with an Agent

Create an Agent and its associated Network, for example a Reddit Search Agent:

import {
  anthropic,
  createAgent,
  createNetwork,
} from "@inngest/agent-kit";

const searchAgent = createAgent({
  name: "reddit_searcher",
  description: "An agent that searches Reddit for relevant information",
  system:
  "You are a helpful assistant that searches Reddit for relevant information.",
});

// Create the network
const redditSearchNetwork = createNetwork({
  name: "reddit_search_network",
  description: "A network that searches Reddit using Browserbase",
  agents: [searchAgent],
  maxIter: 2,
  defaultModel: anthropic({
    model: "claude-3-5-sonnet-latest",
    max_tokens: 4096,
  }),
});

3. Create a Browserbase Tool

Configure the Browserbase SDK and create a tool that can search Reddit:

import {
  anthropic,
  createAgent,
  createNetwork,
  createTool,
} from "@inngest/agent-kit";
import { z } from "zod";
import { chromium } from "playwright-core";
import Browserbase from "@browserbasehq/sdk";

const bb = new Browserbase({
  apiKey: process.env.BROWSERBASE_API_KEY as string,
});

// Create a tool to search Reddit using Browserbase
const searchReddit = createTool({
  name: "search_reddit",
  description: "Search Reddit posts and comments",
  parameters: z.object({
    query: z.string().describe("The search query for Reddit"),
  }),
  handler: async ({ query }, { step }) => {
    return await step?.run("search-on-reddit", async () => {
      // Create a new session
      const session = await bb.sessions.create({
        projectId: process.env.BROWSERBASE_PROJECT_ID as string,
      });

      // Connect to the session
      const browser = await chromium.connectOverCDP(session.connectUrl);
      try {
        const page = await browser.newPage();

        // Construct the search URL
        const searchUrl = `https://search-new.pullpush.io/?type=submission&q=${query}`;

        console.log(searchUrl);

        await page.goto(searchUrl);

        // Wait for results to load
        await page.waitForSelector("div.results", { timeout: 10000 });

        // Extract search results
        const results = await page.evaluate(() => {
          const posts = document.querySelectorAll("div.results div:has(h1)");
          return Array.from(posts).map((post) => ({
            title: post.querySelector("h1")?.textContent?.trim(),
            content: post.querySelector("div")?.textContent?.trim(),
          }));
        });

        console.log("results", JSON.stringify(results, null, 2));

        return results.slice(0, 5); // Return top 5 results
      } finally {
        await browser.close();
      }
    });
  },
});

Configure your BROWSERBASE_API_KEY and BROWSERBASE_PROJECT_ID in the .env file. You can find your API key and project ID from the Browserbase dashboard.

We recommend building tools using Browserbase with Inngest’s step.run() function. This ensures that the tool will only run once across multiple runs.

More information about using step.run() can be found in the Multi steps tools page.

4. Put it all together

Complete example of a Reddit search agent using Browserbase:

// Add the tool to the agent
searchAgent.tools.push(searchReddit);

// Run the agent with a query
const executor = redditSearchNetwork.createExecutor();

// Execute the network with a query
const execution = await executor.execute({
  input: "Find discussions about climate change solutions",
});

console.log(execution.output);

For a complete working example, check out the Reddit Search Agent using Browserbase example.

Next Steps

With this foundation, you can build more advanced web browsing agents:

  • Create agents that can perform complex workflows across multiple websites
  • Build data collection agents that gather information from various sources
  • Develop automation agents that interact with web applications
  • Create testing agents that validate web application functionality

For more examples and documentation, visit:

Was this page helpful?