---
title: "Create a session trait with the Code Assistant"
slug: "create-a-session-trait-with-the-code-assistant"
abstract: "How to quickly create a session trait in Sitecore CDP using the AI Assistant using the Code Assistant."
---
# Create a session trait with the Code Assistant

> [!note]
> Code Assistant is only available if Sitecore Stream is enabled for your organization.

You can use the [Code Assistant](https://doc.sitecore.com/cdp/en/users/sitecore-cdp/ai-in-cdp/code-assistant.html "Code Assistant") to quickly create a session trait in Sitecore CDP to retrieve and store session-based values for individual customers. Whether you’re a developer who wants to save time or a marketer without JavaScript experience, the Code Assistant helps you generate code by simply describing what you need in natural language.

To create a session trait with the Code Assistant:

1. In the navigation menu, click **Developer center** > **Session traits**.
2. Click **Create**. Alternatively, you can [duplicate and edit an existing session trait](https://doc.sitecore.com/cdp/en/users/sitecore-cdp/developer-center/session-traits/manage-session-traits.html#UUID-d6fd963c-d749-e238-9bd3-af2cff77e8fd_section-idm464040820384803337019328643 "Duplicate a session trait").
3. Enter a name for the new session trait and click **Create**.

    Sitecore CDP automatically creates a `[friendlyId](manage-session-traits.html#UUID-d6fd963c-d749-e238-9bd3-af2cff77e8fd_section-idm234411970915559 "Copy the friendly ID of a session trait")` when you name the session trait.

    > [!note]
    > The `friendlyId` identifies the session trait in the guest profile, and you must reference this ID to [access the session trait value](https://doc.sitecore.com/cdp/en/users/sitecore-cdp/developer-center/session-traits/defining,-storing,-and-accessing-session-traits.html#UUID-79889d12-06ee-5109-5ac2-083d17151698_section-idm234360237762829 "Accessing session trait values in Sitecore CDP").
    >
    > You can modify the `friendlyId` while creating the session trait, but after the session trait is activated, the ID can no longer be changed.

4. In the code editor, do one of the following:
    - To start from scratch, in the **JavaScript** section, click **Start coding**.
    - To edit an existing code, click **Edit** ![The edit icon](https://doc.sitecore.com/api/markdown-image/cdp/en/users/sitecore-cdp/images/edit_pencil.png).
5. To open the left-hand pane, click **Code Assistant**.

    ![Click Code Assistant to open the left-hand pane.](https://doc.sitecore.com/api/markdown-image/cdp/en/users/sitecore-cdp/images/AI_Assistant_Session_Trait.png)

6. In the left-hand pane, do one of the following:
    - Enter a prompt describing the logic you want to create. Be specific about the criteria, such as guest attributes (gender, city, country), time frames, and the expected return value. See [Prompting the Code Assistant](https://doc.sitecore.com/cdp/en/users/sitecore-cdp/developer-center/session-traits/code-assistant.html#UUID-4761b59d-a6fb-217e-76e6-25c914dee8ca_section-idm234959680885744 "Prompting the Code Assistant") for best practices.
    - Select one of the [suggested prompts](https://doc.sitecore.com/cdp/en/users/sitecore-cdp/developer-center/session-traits/code-assistant.html#UUID-4761b59d-a6fb-217e-76e6-25c914dee8ca_section-idm234959781212048 "Session traits").

    The Code Assistant generates the code. This might take a few minutes.

    > [!note]
    > The Code Assistant generates code based on your current prompt and any code that's present in the code editor. It does not consider your prompt history or previous codes in the editor.
    >
    > AI responses can vary. Running the same prompt more than once might produce different results.

    After the code is generated, it is labeled with a version number, such as *Code Output 1*.

7. To preview the generated code, click **View**.

    ![The AI-generated code is labeled with a version number in the left-hand pane.](https://doc.sitecore.com/api/markdown-image/cdp/en/users/sitecore-cdp/images/AI_Assistant_Session_Trait_generated_code.png)

    > [!tip]
    > If you've entered multiple prompts, use the **View** buttons to compare different code outputs.

8. In the **Code Output** dialog:
    - To insert the generated code into the editor and replace any existing code, click **Replace code**.
    - To copy the generated code to your clipboard, click **Copy code**.

    > [!important]
    > AI-generated code might not always be accurate. Always review and test the code before using it.

9. Optionally, after inserting the code, you can do the following in the code editor:
    - Make manual changes to the code if needed.
    - Click **Format JS** ![Format JS icon](https://doc.sitecore.com/api/markdown-image/cdp/en/users/sitecore-cdp/images/preetify.png) to format your code changes automatically.
    - Click **Undo** ![undo_icon.png](https://doc.sitecore.com/api/markdown-image/cdp/en/users/sitecore-cdp/images/undo_icon.png) to revert changes.
10. Click **Save**.

After saving, make sure to [test the session trait](https://doc.sitecore.com/cdp/en/users/sitecore-cdp/developer-center/session-traits/test-a-session-trait.html "Test a session trait") before you [activate](https://doc.sitecore.com/cdp/en/users/sitecore-cdp/developer-center/session-traits/activate-a-session-trait.html "Activate a session trait") it.