> ## Documentation Index
> Fetch the complete documentation index at: https://docs.browserbase.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Reddit search agent

> Enable your AI agents to search Reddit with Browserbase's managed headless browsers.

## 1. Install AgentKit

Install AgentKit, Browserbase, and Playwright core:

<Tabs>
  <Tab title="npm">
    ```bash theme={null}
    npm install @inngest/agent-kit @browserbasehq/sdk playwright-core
    ```
  </Tab>

  <Tab title="pnpm">
    ```bash theme={null}
    pnpm add @inngest/agent-kit @browserbasehq/sdk playwright-core
    ```
  </Tab>

  <Tab title="yarn">
    ```bash theme={null}
    yarn add @inngest/agent-kit @browserbasehq/sdk playwright-core
    ```
  </Tab>
</Tabs>

<Note>
  Don't have an existing project? Create a new one with your preferred package manager before continuing.
</Note>

## 2. Set up an AgentKit network with an agent

Create an agent and its associated network, for example a Reddit search agent:

```typescript theme={null}
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-sonnet-4-6",
    max_tokens: 4096,
  }),
});
```

## 3. Create a Browserbase tool

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

```typescript theme={null}
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();

      // 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();
      }
    });
  },
});
```

<Note>
  Configure your `BROWSERBASE_API_KEY` in the `.env` file. You can find your API key in the Browserbase dashboard.
</Note>

Build tools using Browserbase with Inngest's `step.run()` function. This ensures the tool only runs once across multiple runs.

See the [multi-step tools page](https://docs.agentkit.ai/tools/multi-step-tools) for more on `step.run()`.

## 4. Put it all together

Here's the full example:

```typescript theme={null}
// 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](https://github.com/inngest/agentkit/tree/main/examples/reddit-search-agent-browserbase).

## Next steps

<CardGroup cols={3}>
  <Card title="AgentKit docs" icon="book" href="https://docs.agentkit.ai/">
    Full AgentKit SDK reference and examples
  </Card>

  <Card title="Stagehand" icon="wand-magic-sparkles" href="https://docs.stagehand.dev/">
    Autonomous browser automation SDK
  </Card>

  <Card title="Browserbase docs" icon="globe" href="https://docs.browserbase.com/">
    Sessions, proxies, and browser configuration
  </Card>
</CardGroup>
