Skip to main content

Quickstart

This guide walks through a complete first integration with Adstract.

You can integrate in two ways:

  • use the Python SDK; or
  • use the REST API, shown here with JavaScript examples.

Both flows follow the same runtime pattern:

  • send the original prompt to Adstract;
  • receive an enhanced prompt or a fallback outcome;
  • send the final prompt to your LLM; and
  • acknowledge the final model response.

1. Prepare your integration​

python -m pip install adstractai

2. Configure credentials​

Adstract requires an API key in both integration paths.

export ADSTRACT_API_KEY="your-api-key"

3. Run your first full flow​

The examples below show the full runtime flow: enhancement request, LLM call, and acknowledgment.

from adstractai import Adstract
from adstractai.models import AdRequestContext
from openai import OpenAI

client = Adstract()
llm_client = OpenAI()

result = client.request_ad(
prompt="How do I improve analytics in my LLM app?",
context=AdRequestContext(
session_id="session-abc",
user_agent=(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
),
user_ip="203.0.113.10",
),
raise_exception=False,
)

llm_result = llm_client.responses.create(
model="gpt-4.1-mini",
input=result.prompt,
)
llm_response = llm_result.output_text

ack = client.acknowledge(
enhancement_result=result,
llm_response=llm_response,
)

if ack is not None:
print(ack.ad_ack_id)
print(ack.status)

client.close()

4. Required request fields​

Every enhancement request must include:

  • session_id
  • user_agent
  • user_ip

These fields identify the request context and are required in both the SDK flow and the REST API flow.

5. Optional targeting context​

You can pass optional targeting context to improve ad relevance.

from adstractai.models import OptionalContext

result = client.request_ad(
prompt="How do I improve analytics in my LLM app?",
context=AdRequestContext(
session_id="session-abc",
user_agent="Mozilla/5.0 (X11; Linux x86_64)",
user_ip="203.0.113.10",
),
optional_context=OptionalContext(
country="US",
region="California",
city="San Francisco",
age=30,
gender="male",
),
)

6. How to read the result​

The SDK returns structured models:

  • request_ad returns EnhancementResult
  • acknowledge returns AdAckResponse on success
  • acknowledge returns None when enhancement did not succeed and acknowledgment is skipped

7. Where to go next​

  • Pricing for current service pricing.