# Guide

This guide builds on the SDK quickstart and shows you how to model, manage, and experiment on your AI configuration using Hypertune.

You'll learn how to:

* Create custom object types to model your AI configuration
* Create flags that use those custom object types
* Access those flags to retrieve AI configuration in your code
* Update and target your AI configuration
* Run experiments on your AI configuration

## Prerequisites

[Set up Hypertune](https://docs.hypertune.com/getting-started/set-up-hypertune)

## Create custom object types to model your AI configuration

Go to the **Schema** view in the dashboard. Click the **+** button in the top-right of the sidebar. Select **Object**, enter a name, and click **Create**.

<figure><img src="https://2048905609-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWa3rQLiu4JZhBRkiyoKz%2Fuploads%2FKA488xvTAKuErj8bJxwM%2Flocalhost_3000_projects_3357_main_draft_logic_setup%3D0%26selected_schema_type%3D%257B%2522type%2522%253A%2522object%2522%252C%2522name%2522%253A%2522EmailAssistantAIConfig%2522%252C%2522selectedChildName%2522%253Anull%257D%26selected_field_path%3Droot%253EemailAssistantAIConfig%20(3).png?alt=media&#x26;token=aed24b30-ebbc-41a4-a2a2-879f93a495bd" alt=""><figcaption></figcaption></figure>

By default, the new object type has no fields.

Click **+ Add** to add a new field. Enter a name, set its type, and click **Create**.

<figure><img src="https://2048905609-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWa3rQLiu4JZhBRkiyoKz%2Fuploads%2F4cUoIY1xmfJhsTBnVNAy%2Flocalhost_3000_projects_3357_main_draft_logic_setup%3D0%26selected_schema_type%3D%257B%2522type%2522%253A%2522object%2522%252C%2522name%2522%253A%2522EmailAssistantAIConfig%2522%252C%2522selectedChildName%2522%253Anull%257D%26selected_field_path%3Droot%253EemailAssistantAIConfig%20(4).png?alt=media&#x26;token=6ca053df-3f5c-4a9b-9a60-d8053343d799" alt=""><figcaption></figcaption></figure>

Repeat for each field you want to add. You can switch to the code view to make this easier. Then click **Save**.

<figure><img src="https://2048905609-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWa3rQLiu4JZhBRkiyoKz%2Fuploads%2FYQBGSJYWTmvqIDPWgXGH%2Flocalhost_3000_projects_3357_main_draft_logic_setup%3D0%26selected_schema_type%3D%257B%2522type%2522%253A%2522object%2522%252C%2522name%2522%253A%2522EmailAssistantAIConfig%2522%252C%2522selectedChildName%2522%253Anull%257D%26selected_field_path%3Droot%253EemailAssistantAIConfig%20(5).png?alt=media&#x26;token=461c1d93-b77d-4cda-82e9-17c7e0d07889" alt=""><figcaption></figcaption></figure>

```graphql
type EmailAssistantAIConfig {
  model: String!
  system: String!
  prompt(email: String!, tone: String!): String!
  maxOutputTokens: Int!
  temperature: Float!
  presencePenalty: Float!
  frequencyPenalty: Float!
  maxRetries: Int!
}
```

Note how the example above has `email` and `tone` arguments on the `prompt` flag. They are passed when calling the `prompt` flag in your code, and can be referenced as variables when writing the prompt in the Hypertune dashboard, enabling you to create a prompt template.

## Create flags for your AI configuration

Go to the **Flags** view in the dashboard. Click the **+** button in the top-right of the sidebar and select **Flag**.

<figure><img src="https://2048905609-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWa3rQLiu4JZhBRkiyoKz%2Fuploads%2FTurwFPeTtaLThUVWeg8r%2Flocalhost_3000_projects_6715_main_draft_schema_setup%3D0%26selected_field_path%3Droot%26selected_schema_type%3D%257B%2522type%2522%253A%2522object%2522%252C%2522name%2522%253A%2522EmailAssistantAIConfig%2522%252C%2522selectedChildName%2522%253Anull%252C%2522count%2522%253A1%257D%26schema_mode%3D.png?alt=media&#x26;token=fb0e6870-e5b8-4e4b-927e-ec93a6290961" alt=""><figcaption></figcaption></figure>

Enter a name, set its type to the one you created earlier, and click **Create**.

<figure><img src="https://2048905609-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWa3rQLiu4JZhBRkiyoKz%2Fuploads%2FuS2Li4kzqjHcXZSXWB24%2Flocalhost_3000_projects_6715_main_draft_schema_setup%3D0%26selected_field_path%3Droot%26selected_schema_type%3D%257B%2522type%2522%253A%2522object%2522%252C%2522name%2522%253A%2522EmailAssistantAIConfig%2522%252C%2522selectedChildName%2522%253Anull%252C%2522count%2522%253A1%257D%26schema_m%20(1).png?alt=media&#x26;token=a15429a1-597b-4d47-8e3f-30b03aed6470" alt=""><figcaption></figcaption></figure>

Enter the initial configuration, then click **Save**.

<figure><img src="https://2048905609-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWa3rQLiu4JZhBRkiyoKz%2Fuploads%2FHAhR0Ey440z5OxujPi2j%2Flocalhost_3000_projects_6715_main_draft_logic_setup%3D0%26selected_field_path%3Droot%253EemailAssistantAIConfig%26selected_schema_type%3D%257B%2522type%2522%253A%2522object%2522%252C%2522name%2522%253A%2522EmailAssistantAIConfig%2522%252C%2522selectedChildName%2522%253Anull%252C%2522coun.png?alt=media&#x26;token=be5c9978-b447-47e8-b3f8-6678be1812c7" alt=""><figcaption></figcaption></figure>

## Access flags to retrieve AI configuration

Regenerate the client:

{% tabs %}
{% tab title="npm" %}

```bash
npx hypertune
```

{% endtab %}

{% tab title="yarn" %}

```bash
yarn hypertune
```

{% endtab %}

{% tab title="pnpm" %}

```bash
pnpm hypertune
```

{% endtab %}
{% endtabs %}

Then use the generated methods to access your flags:

{% code title="app/api/completion/route.ts" %}

```typescript
import { waitUntil } from '@vercel/functions'
import { generateText } from 'ai'
import getHypertune from '@/lib/getHypertune'

export async function POST(req: Request) {
  const hypertune = await getHypertune({ isRouteHandler: true })

  const aiConfig = hypertune.emailAssistantAIConfig()

  const { email, tone }: { email: string; tone: string } =
    await req.json()

  const { text } = await generateText({
    model: aiConfig.model({ fallback: 'openai/gpt-4.1' }),
    system: aiConfig.system({
      fallback: `You are a professional assistant that drafts clear, polite, and concise email replies for a busy executive.`,
    }),
    prompt: aiConfig.prompt({
      args: { email, tone },
      fallback: `Write a reply to the following email:\n\n${email}\n\nThe tone should be ${tone} and the response should address all points mentioned.`,
    }),
    maxOutputTokens: aiConfig.maxOutputTokens({ fallback: 400 }),
    temperature: aiConfig.temperature({ fallback: 0.5 }),
    presencePenalty: aiConfig.presencePenalty({ fallback: 0.1 }),
    frequencyPenalty: aiConfig.frequencyPenalty({
      fallback: 0.3,
    }),
    maxRetries: aiConfig.maxRetries({ fallback: 5 }),
  })

  waitUntil(hypertune.flushLogs())

  return Response.json({ text })
}
```

{% endcode %}

## Update your AI configuration

Go to the **Flags** view in the dashboard, and select the flag with your AI configuration from the left sidebar.

<figure><img src="https://2048905609-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWa3rQLiu4JZhBRkiyoKz%2Fuploads%2Fz87ica4LkS1FL8dHj2Xj%2Flocalhost_3000_projects_6715_main_draft_logic_setup%3D0%26selected_field_path%3Droot%253EemailAssistantAIConfig%26selected_schema_type%3D%257B%2522type%2522%253A%2522object%2522%252C%2522name%2522%253A%2522EmailAssistantAIConfig%2522%252C%2522selectedChildName%2522%253Anull%252C%2522%20(1).png?alt=media&#x26;token=ce82de93-5df6-42fd-a1c0-2876ce00839b" alt=""><figcaption></figcaption></figure>

Make your changes, then open the **Diff** view to review them. Click **Save**.

<figure><img src="https://2048905609-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWa3rQLiu4JZhBRkiyoKz%2Fuploads%2FtmfZzm2onfK3bttWOt3M%2Flocalhost_3000_projects_3357_main_draft_diff_setup%3D0%26selected_schema_type%3D%257B%2522type%2522%253A%2522object%2522%252C%2522name%2522%253A%2522EmailAssistantAIConfig%2522%252C%2522selectedChildName%2522%253Anull%252C%2522count%2522%253A1%257D%26schema_mode%3Dcode%26selected_field_path%3Dro.png?alt=media&#x26;token=b6850f51-ce57-4595-ac00-a2853ae3581d" alt=""><figcaption></figcaption></figure>

## Target your AI configuration

Go to the **Flags** view in the dashboard, and select the flag with your AI configuration from the left sidebar. Click the arrow (**>**) to view each subfield in the sidebar, and select the field you want to add a targeting rule to.

<figure><img src="https://2048905609-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWa3rQLiu4JZhBRkiyoKz%2Fuploads%2FO9LJ6LfFFfcDfHi3wi8y%2Flocalhost_3000_projects_6715_main_draft_logic_setup%3D0%26selected_field_path%3Droot%253EemailAssistantAIConfig%26selected_schema_type%3D%257B%2522type%2522%253A%2522object%2522%252C%2522name%2522%253A%2522EmailAssistantAIConfig%2522%252C%2522selectedChildName%2522%253Anull%252C%2522%20(5).png?alt=media&#x26;token=5dfeff94-10a7-455e-af55-b83e4e67564f" alt=""><figcaption></figcaption></figure>

Click **+ Rule**, set your condition, provide alternate configuration for that condition, and click **Save**.

<figure><img src="https://2048905609-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWa3rQLiu4JZhBRkiyoKz%2Fuploads%2FWwpA8gil2ByajUWW38e2%2Flocalhost_3000_projects_6715_main_draft_logic_setup%3D0%26selected_field_path%3Droot%253EemailAssistantAIConfig%26selected_schema_type%3D%257B%2522type%2522%253A%2522object%2522%252C%2522name%2522%253A%2522EmailAssistantAIConfig%2522%252C%2522selectedChildName%2522%253Anull%252C%2522%20(4).png?alt=media&#x26;token=dbd10672-3832-475e-8908-3f27ceab9883" alt=""><figcaption></figcaption></figure>

## Experiment on your AI configuration

Go to the **Flags** view in the dashboard, and select the flag with your AI configuration from the left sidebar. Click the arrow (**>**) to view each subfield in the sidebar, and select the field you want to add a targeting rule to.

<figure><img src="https://2048905609-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWa3rQLiu4JZhBRkiyoKz%2Fuploads%2Fct1lQmLo1BtmLsjdpWXO%2Flocalhost_3000_projects_6715_main_draft_logic_setup%3D0%26selected_field_path%3Droot%253EemailAssistantAIConfig%26selected_schema_type%3D%257B%2522type%2522%253A%2522object%2522%252C%2522name%2522%253A%2522EmailAssistantAIConfig%2522%252C%2522selectedChildName%2522%253Anull%252C%2522%20(5).png?alt=media&#x26;token=c2b0b562-7485-47bd-8a56-6975ff068acc" alt=""><figcaption></figcaption></figure>

Click **+ Experiment**. In the dropdown, select **New experiment**.

<figure><img src="https://2048905609-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWa3rQLiu4JZhBRkiyoKz%2Fuploads%2FpbaNbyhZqDxkTkesbmE8%2Flocalhost_3000_projects_6715_main_draft_logic_setup%3D0%26selected_field_path%3Droot%253EemailAssistantAIConfig%26selected_schema_type%3D%257B%2522type%2522%253A%2522object%2522%252C%2522name%2522%253A%2522EmailAssistantAIConfig%2522%252C%2522selectedChildName%2522%253Anull%252C%2522%20(6).png?alt=media&#x26;token=076a3794-5a6c-44ea-bfec-36d52985f653" alt=""><figcaption></figcaption></figure>

Enter a name for your experiment and click **Create**.

<figure><img src="https://2048905609-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWa3rQLiu4JZhBRkiyoKz%2Fuploads%2FQHQFMVcs9QARzPfiTOzh%2Flocalhost_3000_projects_6715_main_draft_logic_setup%3D0%26selected_field_path%3Droot%253EemailAssistantAIConfig%26selected_schema_type%3D%257B%2522type%2522%253A%2522object%2522%252C%2522name%2522%253A%2522EmailAssistantAIConfig%2522%252C%2522selectedChildName%2522%253Anull%252C%2522%20(7).png?alt=media&#x26;token=40a7b65e-b18d-48ac-91a6-41c7d236cdf9" alt=""><figcaption></figcaption></figure>

Click **Insert** and update the **Test** variant of your content.

<figure><img src="https://2048905609-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWa3rQLiu4JZhBRkiyoKz%2Fuploads%2Fxjzkonk0mg2E0FRHPhGk%2Flocalhost_3000_projects_6715_main_draft_logic_setup%3D0%26selected_field_path%3Droot%253EemailAssistantAIConfig%26selected_schema_type%3D%257B%2522type%2522%253A%2522object%2522%252C%2522name%2522%253A%2522EmailAssistantAIConfig%2522%252C%2522selectedChildName%2522%253Anull%252C%2522%20(8).png?alt=media&#x26;token=b842dab9-8aab-4ea8-a7cd-d4c273f5f584" alt=""><figcaption></figcaption></figure>

Review your changes in the **Diff** view and click **Save**.

<figure><img src="https://2048905609-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWa3rQLiu4JZhBRkiyoKz%2Fuploads%2FeRWMuFHw5a7ebsgSJ3mw%2Flocalhost_3000_projects_6715_main_draft_logic_setup%3D0%26selected_field_path%3Droot%253EemailAssistantAIConfig%26selected_schema_type%3D%257B%2522type%2522%253A%2522object%2522%252C%2522name%2522%253A%2522EmailAssistantAIConfig%2522%252C%2522selectedChildName%2522%253Anull%252C%2522%20(9).png?alt=media&#x26;token=d7cf25d9-cc18-465c-8bbd-7093789c929e" alt=""><figcaption></figcaption></figure>

Once you've [analyzed your experiment results](https://docs.hypertune.com/experimentation/guide#analyze-experiment-results) and decided on a winning variant, go to the **Flags** view and select your flag. Click the options button (⋯) next to the variant you want to ship and select **Ship variant**, then click **Save**.

<figure><img src="https://2048905609-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWa3rQLiu4JZhBRkiyoKz%2Fuploads%2FitokSGqKQj83jpc2kLmH%2Flocalhost_3000_projects_6715_main_draft_logic_setup%3D0%26selected_field_path%3Droot%253EemailAssistantAIConfig%26selected_schema_type%3D%257B%2522type%2522%253A%2522object%2522%252C%2522name%2522%253A%2522EmailAssistantAIConfig%2522%252C%2522selectedChildName%2522%253Anull%252C%252%20(13).png?alt=media&#x26;token=405f859c-7685-46de-8def-e224f750fe69" alt=""><figcaption></figcaption></figure>

## Next steps

* Run a [multivariate test](https://docs.hypertune.com/concepts/multivariate-tests) to find the best combination of model choice, prompts, and settings to optimize key metrics like cost, latency, or user satisfaction.
* Set up an [AI loop](https://docs.hypertune.com/concepts/ai-loops) to automatically optimize AI configuration for each unique user to optimize key metrics.
* Extend your schema to support more complex AI configuration, e.g. prompt chains.
