Get API key

Stable Diffusion Prompt Generator LLM: Python Setup

Updated

If you want a stable diffusion prompt generator llm to expand a few words into a full, well-structured prompt, you need only a text model and about thirty lines of Python. The model writes the prompt as text; it does not render pictures, so you still run the result through SDXL or whichever image tool you use.

Glass funnel turning a few glowing words into a long ribbon of detail heading to a blank canvas
Diorama of a small stage with glowing speech bubbles representing a text roleplay scenario

Key points

  • The LLM only writes text. Feed its output into your own SDXL, ComfyUI or Automatic1111 setup.
  • JSON mode returns a positive prompt and a negative prompt you can parse without regex.
  • One expansion costs under $0.0001, so 1,000 prompts cost about $0.09.
  • Keep prompts concise: SDXL's text encoders read only the first 77 tokens of each chunk.

What the LLM does and what it does not

Image models respond well to dense, specific prompts: subject, setting, lens, lighting, medium, mood, and a list of things to avoid. Most people type three words and get a flat result. An LLM closes that gap by taking your short idea and writing the long version in a consistent format.

Be clear about the division of labour. This service is a text-only API. It does not generate images, audio or video, and it has no embeddings or fine-tuning. It sits in front of your image pipeline and writes the instructions that pipeline reads. It is an OpenAI-compatible endpoint serving one open-weight model from an independent provider, not a product of any image-model vendor.

The model is tuned to answer without content refusals for lawful adult use, which matters for artists and game developers whose subjects (horror, violence in fiction, mature themes) get blocked elsewhere. Sexual content involving minors is a hard limit and is never produced.

Step 1: get a key and test one call

Sign up with Google or with email and password, then open the API key page. A one-click trial adds $0.50 of credit, valid for 7 days and needing no card. That is enough for thousands of prompt expansions, as the cost section shows.

Check that everything works with curl:

curl https://api.sdxlapi.com/v1/chat/completions \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "uncensored",
    "messages": [
      {"role": "system", "content": "You write Stable Diffusion prompts."},
      {"role": "user", "content": "lighthouse in a storm"}
    ]
  }'

Any OpenAI SDK works once you change the base URL and key. Details are in the documentation.

Step 2: a system prompt that gives stable output

The system prompt is where quality comes from. Tell the model the target format, the order of elements, a length limit and what to leave out. A version that works well as a starting point:

You turn short image ideas into prompts for SDXL. Write one positive prompt of 40 to 60 words: subject first, then setting, composition, lighting, medium or style, and mood. Use comma-separated phrases, no full sentences. Then write a negative prompt of up to 15 short phrases. Do not add text, watermarks or captions to the picture. Return JSON with the keys "prompt" and "negative".

The word limit matters. SDXL's CLIP text encoders read prompts in chunks of 77 tokens, and anything past that is either cut or handled by a chunking feature in your UI. Aiming for 40 to 60 words keeps most prompts inside one chunk, with the most important details first.

Use a low-to-medium temperature (0.7 to 0.9) so the style stays consistent but two runs of the same idea differ. If you need identical output for testing, pass a fixed seed.

Step 3: the Python script

This script reads ideas from a list, asks for JSON, and prints the prompt pair. It uses the official OpenAI Python package with the base URL changed.

import json
from openai import OpenAI

client = OpenAI(base_url="https://api.sdxlapi.com/v1", api_key="YOUR_KEY")

SYSTEM = (
    "You turn short image ideas into prompts for SDXL. Write one positive "
    "prompt of 40 to 60 words: subject, setting, composition, lighting, "
    "medium or style, mood, as comma-separated phrases. Then a negative "
    "prompt of up to 15 short phrases. Return JSON with keys "
    "'prompt' and 'negative'."
)

def expand(idea: str) -> dict:
    resp = client.chat.completions.create(
        model="uncensored",
        messages=[
            {"role": "system", "content": SYSTEM},
            {"role": "user", "content": idea},
        ],
        response_format={"type": "json_object"},
        temperature=0.8,
        max_tokens=400,
    )
    return json.loads(resp.choices[0].message.content)

for idea in ["lighthouse in a storm", "abandoned greenhouse at dawn"]:
    result = expand(idea)
    print(idea, "->", result["prompt"])
    print("negative:", result["negative"], "\n")

JSON mode (response_format with json_object) makes the reply parse cleanly. Still wrap json.loads in a try block in production and retry once if it fails, because a long reply cut at max_tokens can end mid-object.

Step 4: send prompts to your image tool

The output is plain strings, so any front end can take it. With a local Automatic1111 or ComfyUI install, post the two fields to its API. With a script that loops over a CSV of ideas, write the results to a file and queue them as a batch.

  • Save the idea, prompt and negative prompt together, so you can trace a good image back to the instruction that produced it.
  • Add a style line from your own list ("cinematic photograph", "ink illustration") as an extra field in the user message when you want a series with a matching look.
  • Keep a short library of prompts that worked, and feed two or three of them into the system prompt as examples. Few-shot examples steer style more reliably than adjectives do.

For a roleplay or game project, the same pattern can illustrate scenes from a story, with the LLM turning the last few paragraphs into a visual description. The article on text-based roleplay scenarios covers the story side.

Common problems and fixes

A few issues come up in almost every prompt-generator project.

The model adds commentary

Replies that start with "Here is your prompt" break pipelines. JSON mode removes most of this. If you work in plain text, say "output only the prompt, no preamble" and add a stop sequence for a line you never want to see.

Every prompt looks the same

Low temperature and a strict template give uniform results. Raise temperature to 1.0, vary the style line you pass in, or ask for three alternatives and pick one. Penalties (frequency_penalty, presence_penalty) are passed through and can break up repeated phrases.

Prompts are too long for the image model

Put a hard word limit in the system prompt and check length in code. Truncating by hand drops the last details, which are usually the least important, but it is better to have the model write short in the first place.

You hit a 429

You sent too many requests per minute or at once. Back off for a few seconds and retry; the response may include Retry-After: 5. A batch with a small thread pool of 4 to 8 workers stays within a full key's limits.

What 1,000 prompts cost

Pricing is $0.15 per 1M input tokens and $0.50 per 1M output tokens. A typical expansion sends a system prompt of about 150 tokens plus a 15-token idea, call it 170 tokens in, and receives about 120 tokens of JSON.

  • Input: 170 × $0.15 / 1M = $0.0000255.
  • Output: 120 × $0.50 / 1M = $0.00006.
  • Per prompt: about $0.0000855.
  • 1,000 prompts: about $0.0855, or $0.09.

Round it up for longer system prompts or few-shot examples. If you add 600 tokens of examples, input becomes 770 tokens and 1,000 prompts cost about $0.1155 for input + $0.06 for output, roughly $0.18 in total. Either way, the $0.50 trial covers several thousand expansions. Failed requests are not charged, and paid credit never expires; see the pricing page for top-ups ($20 to $500, by card or crypto).

Questions and answers

Can the LLM generate the Stable Diffusion image itself?

No. It writes text only. You pass its prompt to SDXL, ComfyUI, Automatic1111 or any other image tool you run.

How long should an SDXL prompt be?

Around 40 to 60 words works well, with key details first, because the text encoders read in 77-token chunks.

Does it refuse mature or dark themes?

The model is tuned to answer without content refusals for lawful adult use. Content involving minors is never produced.

Which model name do I send?

Send uncensored as the model ID, with the base URL https://api.sdxlapi.com/v1 and your key as a Bearer token.

Can I run many prompts in parallel?

A full key allows 8 concurrent requests and 300 per minute; a trial key allows 2 and 60. A 429 response means wait and retry.

Your key is one form away

Create an account, copy the key, change the base URL. That is the whole setup.

Get API key