> For the complete documentation index, see [llms.txt](https://doc.wearepatchworks.com/product-documentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://doc.wearepatchworks.com/product-documentation/developer-hub/custom-scripting/creating-and-testing-custom-scripts/creating-a-script-with-ai.md).

# Creating a script with AI

## Introduction

The custom script editor includes an integrated AI agent, so you can get real-time assistance creating and updating scripts!

Our AI integration knows about expected keys and value types (payload, variables, meta, etc.), so generated scripts will be in a form that's ready to use in process flows.

{% hint style="info" %}
For the best results from AI, use PHP, JavaScript, Python, or GO. AI does work for other languages however, you will need to provide significantly more context in your requests.
{% endhint %}

<details>

<summary><img src="/files/JOYoP4EdSu7WJ3CMdAAS" alt="" data-size="line"> Show me</summary>

<figure><img src="/files/EwCJak8BmoWKGmNykdbm" alt=""><figcaption></figcaption></figure>

</details>

## Need to know

* The maximum memory size for a custom script is 512MB
* The maximum size of a custom script is 4GB
* Up to 5 AI requests can be made per day for any company profile, free of charge.
* A [subscription tier bolt-on](/product-documentation/getting-started/subscription-tier-limits.md) can be purchased if you'd like to increase this allowance.
* You can view the number of AI requests used and remaining from your [AI conversation history](/product-documentation/developer-hub/custom-scripting/creating-and-testing-custom-scripts/creating-a-script-with-ai/ai-conversation-history.md).
* AI prompts can be up to 1500 characters.
* Sample payloads for an AI request can be up to 6000 characters.

## Using AI to create a script

Follow the steps below:

**Step 1**\
Log in to the [Patchworks dashboard](https://app.wearepatchworks.com/scripts) and select `scripts` from the left-hand navigation bar.

**Step 2**\
Select the `create script` button:

<div align="left"><figure><img src="/files/pyjh4dY9D1HkxTZ5CYD4" alt=""><figcaption></figcaption></figure></div>

**Step 3**\
Add a unique `name` and a `description` and then choose your preferred coding language:

<div align="left"><figure><img src="/files/ySKqXyJB8dRPh1q28CAf" alt=""><figcaption></figcaption></figure></div>

{% hint style="warning" %}
Script names must be unique - you can't add two scripts with the same name.
{% endhint %}

**Step 4**\
Move down to the Patchworks AI panel and enter your prompt (be as clear and specific as possible) - for example:

<div align="left"><figure><img src="/files/1p8nYC1dHjjLk9rWaW0m" alt=""><figcaption></figcaption></figure></div>

{% hint style="info" %}
The Patchworks AI panel is displayed as soon as your coding language is selected in the previous step.
{% endhint %}

**Step 5**\
If you have a sample payload and would like to provide it for clarity, select the `provide an example payload?` checkbox and paste in your payload - for example:

<div align="left"><figure><img src="/files/820nkCATTJmMKFnolSuJ" alt=""><figcaption></figcaption></figure></div>

We recommend providing a sample payload whenever possible, as it provides valuable context to the AI agent.

Sample payloads don't have to be perfect (or valid code) - as long as the structure and format are described correctly, the AI agent should have enough to work with. For example, if you don't have a JSON payload available, you might enter something like:

{% code overflow="wrap" %}

```
The payload comprises a JSON object containing multiple fields and associated values. There are no arrays. 
```

{% endcode %}

**Step 6**\
Click the `generate` button:

<div align="left"><figure><img src="/files/ySPMra2KXudsettwbRGb" alt=""><figcaption></figcaption></figure></div>

{% hint style="warning" %}
Each time you `generate` a script, the [AI request allowance associated with your subscription tier](/product-documentation/getting-started/subscription-tier-limits.md) is decremented. You can view the number of AI requests used and remaining from your [AI conversation history](/product-documentation/developer-hub/custom-scripting/creating-and-testing-custom-scripts/creating-a-script-with-ai/ai-conversation-history.md).
{% endhint %}

**Step 7**\
AI generates a first pass of the script, which is displayed in an overlay:

<div align="left"><figure><img src="/files/vEiibKDHmnFVkm1oGLXi" alt=""><figcaption></figcaption></figure></div>

{% hint style="info" %}
If your request is ambiguous (for example, you indicate that you require some specific fields to be added but don't stipulate what these are), you may be prompted for clarification:\
\
![](/files/LKAkfnGp49xh3cjwAy4D)

In this case, enter your response in the comments field and click `send reply`.<br>
{% endhint %}

From here:

* If you are satisfied that the script is correct, or that you at least have enough to work with independently, click `accept script`:\
  \ <img src="/files/ZNXkOSvHrP3v0Df20KDG" alt="" data-size="original"><br>

  <div data-gb-custom-block data-tag="hint" data-style="info" class="hint hint-info"><p>You can't edit the generated script in the AI 'overlay' window - you need to accept it and then make any required updates in the <a href="/pages/cqSzgwlcW3Ylc5t53EWt#adding-and-editing-script-code">standard script editor</a>.</p></div>

  \
  Having accepted a script, it opens in the [standard script editor](/product-documentation/developer-hub/custom-scripting/creating-and-testing-custom-scripts/creating-a-script-manually.md#adding-and-editing-script-code) so you can [save](/product-documentation/developer-hub/custom-scripting/creating-and-testing-custom-scripts/creating-a-script-manually.md#adding-and-editing-script-code), [edit](/product-documentation/developer-hub/custom-scripting/creating-and-testing-custom-scripts/creating-a-script-manually.md#adding-and-editing-script-code) and [test](/product-documentation/developer-hub/custom-scripting/creating-and-testing-custom-scripts/testing-scripts.md) the code as needed.

  <div data-gb-custom-block data-tag="hint" data-style="warning" class="hint hint-warning"><p>Always <a href="/pages/mXsuWgXHXy9k2wPofo1e">test</a> your scripts to ensure that the results are as expected.</p></div>
* If you want to refine your requirements and ask AI to generate again, update the `comments` field and click `send reply`:\
  \
  ![](/files/bKHZI716TuSzWWFYnhrl)<br>

  <div data-gb-custom-block data-tag="hint" data-style="warning" class="hint hint-warning"><p>Each time you refine the generated output with <code>send reply</code>, the <a href="/pages/5JzXXY339CKzG84bKWYR">AI request allowance associated with your subscription tier</a> is decremented. You can view the number of AI requests used and remaining from your <a href="/pages/XfLA64OR7JI7caeHAkvU">AI conversation history</a>.</p></div>

  \
  Then, if you are satisfied that the script is correct, or that you at least have enough to work with independently, click `accept script` - otherwise you can refine your requirements again.
* If you want to close the script and exit back to your original prompt, click `close`.<br>
* If you feel that the generated response is not appropriate/helpful, click `report`. This marks the conversation for review by Patchworks.
