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

**1. 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.

![Completed Agent run with an arrow pointing to the Optimize button](https://mintcdn.com/browserbase/Bj3ZWqgjlTbkCOGW/images/platform/agents/generating-scripts/open-agent-run.png?fit=max\&auto=format\&n=Bj3ZWqgjlTbkCOGW\&q=85\&s=753935981c2f6c5aeb5472ce8693f05a)

**2. 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.

**3. 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.

![Optimize panel with an arrow pointing to the Write a script starter prompt](https://mintcdn.com/browserbase/Bj3ZWqgjlTbkCOGW/images/platform/agents/generating-scripts/write-script-prompt.png?fit=max\&auto=format\&n=Bj3ZWqgjlTbkCOGW\&q=85\&s=8e5afac25c55e2435ecfe1ce8f347a25)

**4. 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.

![Optimize response with an arrow pointing to the generated script](https://mintcdn.com/browserbase/Bj3ZWqgjlTbkCOGW/images/platform/agents/generating-scripts/generated-script.png?fit=max\&auto=format\&n=Bj3ZWqgjlTbkCOGW\&q=85\&s=e6c20376c2cd1422ba110324d9653876)

**5. Run the script with Model Gateway**

Follow the [Stagehand quickstart](https://docs.browserbase.com/welcome/quickstarts/stagehand.md) to install Stagehand, set `BROWSERBASE_API_KEY`, and run the generated file.

Stagehand automatically uses [Model Gateway](https://docs.browserbase.com/platform/model-gateway/overview.md) 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.

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

> [card] [Stagehand quickstart](https://docs.browserbase.com/welcome/quickstarts/stagehand.md)
>
> Install Stagehand and run your generated script

> [card] [Model Gateway](https://docs.browserbase.com/platform/model-gateway/overview.md)
>
> Use models through your Browserbase API key

> [card] [Optimize an Agent](https://docs.browserbase.com/platform/agents/optimizing-agents.md)
>
> Improve an Agent from its run history

> [card] [Integrate an Agent](https://docs.browserbase.com/platform/agents/integrate-api-sdk.md)
>
> Trigger Agent runs from your application


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.