---
title: "AI generation"
description: "Configure optional AI-assisted activity generation in Open edX Studio."
source: "https://docs.blend-ed.com/interactive-content-xblock/ai-generation/"
---

# AI generation

AI generation is optional and off by default. When configured, Studio authors can describe an activity and generate HTML, CSS, JavaScript, a display name, and grading metadata. They can also refine the current content.

Generated fields are placed in the editor for review. Nothing is persisted until the author selects **Save**.

## Configure an OpenAI-compatible endpoint
Add the setting to both LMS and CMS configuration under `XBLOCK_SETTINGS`:

```
XBLOCK_SETTINGS:  InteractiveContentXBlock:    ai:      api_url: "https://api.openai.com/v1/chat/completions"      api_key: "${INTERACTIVE_CONTENT_AI_API_KEY}"      model: "gpt-4.1"      timeout: 120      temperature: 0.7      max_tokens: 8192      extra_headers: {}
```
The endpoint must implement the OpenAI-compatible chat completions response shape. `api_url` or a custom `provider` enables the Studio section. No AI SDK or additional Python dependency is installed by the XBlock.

> **Caution**
> Keep credentials in your deployment’s secret store. The example uses a placeholder environment reference; do not commit a real API key to Tutor config or source control.

## Use a custom provider
Deployments can route generation through their own gateway, credential system, or usage accounting by configuring a dotted Python callable:

```
XBLOCK_SETTINGS:  InteractiveContentXBlock:    ai:      provider: "myplugin.ai.generate_for_interactive_xblock"      model: "deployment-specific-model"
```
The function receives keyword arguments:

```
def generate_for_interactive_xblock(*, prompt, config, context):    # context contains usage_id, user_id, and username    return raw_model_response
```
Return the raw model text in the format requested by the prompt: fenced `html`, `css`, `js`, and `json` blocks. The JSON metadata can include `display_name` and `correct_answers`.

## Author workflow
1. Open **Generate with AI** in the Studio editor.
2. Describe one focused inline learning interaction.
3. Select **Refine current content** when you want to preserve and modify the
current component.
4. Generate, then review all HTML, CSS, JavaScript, and grading fields.
5. Run **Test Grading** .
6. Save and test the published unit as a learner.

AI generation automatically enables grade publication because generated JavaScript is required to call `submitInteraction()` with correctness data.

## Security and cost controls
- Only a user with course write permission in Studio can call the generation
handler. It fails closed in the LMS, XBlock Workbench, and runtimes without the
Studio permissions service.
- The API key stays on the server and is not sent to the browser.
- Author prompts are capped at 8,000 characters. Each current content field is
capped at 40,000 characters during refinement, and the current title at 1,000.
- The generated response must contain HTML and JavaScript, call `submitInteraction()` , and keep `<script>` and `<style>` elements out of the
HTML field.
- Generated code still runs as trusted author code in the LMS. Human review is
required before publishing.

The default request timeout is 120 seconds, temperature is `0.7`, and maximum output is 8,192 tokens. Set deployment-appropriate budgets and upstream rate limits; the XBlock does not provide per-user quotas.
