> ## 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.

# Generate a script

> Turn a completed Agent run into a reusable Stagehand script with the Optimize tool.

Use the Optimize tool to turn a completed Agent run into a reusable [Stagehand](https://docs.stagehand.dev/) script. Optimize reads the run's steps and generates code that follows the same workflow.

This works best after your Agent completes the task successfully. The completed run gives Optimize a working path to translate into code.

## Generate the script

<Steps>
  <Step title="Open a completed Agent run">
    Open the [Browserbase dashboard](https://www.browserbase.com/overview), select **Agents**, and choose the completed run you want to reproduce.

    <Frame>
      <img src="https://mintcdn.com/browserbase/Bj3ZWqgjlTbkCOGW/images/platform/agents/generating-scripts/open-agent-run.png?fit=max&auto=format&n=Bj3ZWqgjlTbkCOGW&q=85&s=753935981c2f6c5aeb5472ce8693f05a" alt="Completed Agent run with an arrow pointing to the Optimize button" width="1591" height="849" data-path="images/platform/agents/generating-scripts/open-agent-run.png" />
    </Frame>
  </Step>

  <Step title="Open Optimize">
    Click **Optimize** in the run view. Optimize uses the run's event history, so you don't need to describe every action again.
  </Step>

  <Step title="Ask for a Stagehand script">
    Enter this prompt in **Ask about this session**:

    ```text theme={null}
    Can you create a Stagehand script to do the same thing as this Agent run?
    ```

    You can also select **Write a script** from the starter prompts.

    <Frame>
      <img src="https://mintcdn.com/browserbase/Bj3ZWqgjlTbkCOGW/images/platform/agents/generating-scripts/write-script-prompt.png?fit=max&auto=format&n=Bj3ZWqgjlTbkCOGW&q=85&s=8e5afac25c55e2435ecfe1ce8f347a25" alt="Optimize panel with an arrow pointing to the Write a script starter prompt" width="1590" height="849" data-path="images/platform/agents/generating-scripts/write-script-prompt.png" />
    </Frame>
  </Step>

  <Step title="Review and copy the script">
    Optimize returns a Stagehand script based on the run. Review the URLs, actions, extraction logic, and expected result, then copy the code into your project.

    Treat the generated script as a starting point. Run it against the same workflow before you use it with new inputs or sites.

    <Frame>
      <img src="https://mintcdn.com/browserbase/Bj3ZWqgjlTbkCOGW/images/platform/agents/generating-scripts/generated-script.png?fit=max&auto=format&n=Bj3ZWqgjlTbkCOGW&q=85&s=e6c20376c2cd1422ba110324d9653876" alt="Optimize response with an arrow pointing to the generated script" width="1591" height="849" data-path="images/platform/agents/generating-scripts/generated-script.png" />
    </Frame>
  </Step>

  <Step title="Run the script with Model Gateway">
    Follow the [Stagehand quickstart](/welcome/quickstarts/stagehand) to install Stagehand, set `BROWSERBASE_API_KEY`, and run the generated file.

    Stagehand automatically uses [Model Gateway](/platform/model-gateway/overview) when you use a Browserbase-hosted browser without a provider-specific API key. You can run the generated script with one Browserbase API key and no separate model provider account.
  </Step>
</Steps>

## When to generate a script

Generate a script when you have a successful Agent run and want more control over how the workflow executes. A script lets you:

* Edit individual Stagehand or Playwright steps.
* Add the workflow to an existing application.
* Test changes with your development tools.
* Reuse a known path for repeated tasks.

Keep using an Agent when the workflow needs to adapt across sites, layouts, or tasks that you can't define ahead of time.

## Next steps

<CardGroup cols={2}>
  <Card title="Stagehand quickstart" icon="wand-magic-sparkles" iconType="sharp-solid" href="/welcome/quickstarts/stagehand">
    Install Stagehand and run your generated script
  </Card>

  <Card title="Model Gateway" icon="route" iconType="sharp-solid" href="/platform/model-gateway/overview">
    Use models through your Browserbase API key
  </Card>

  <Card title="Optimize an Agent" icon="gauge-high" iconType="sharp-solid" href="/platform/agents/optimizing-agents">
    Improve an Agent from its run history
  </Card>

  <Card title="Integrate an Agent" icon="plug" iconType="sharp-solid" href="/platform/agents/integrate-api-sdk">
    Trigger Agent runs from your application
  </Card>
</CardGroup>
