Skip to main content

Lakera Red SDK

Official Python SDK for Lakera Red — run adversarial scans against your AI agents from your own runtime.

Lakera is a Check Point company.

Install

pip install lakera-red-sdk

Quick start

import asyncio
from lakera_red_sdk import LakeraRedClient, Session


async def main():
    async with LakeraRedClient(
        api_key="sk_lr_...",
        base_url="https://red-webhooks.lakera.ai",
        log_level="info",
    ) as client:

        async def handler(session: Session) -> None:
            async for message in session:
                reply = await your_agent(session.id, message.attack)
                await message.respond(reply)

        # A target owns its recon profile. create_or_get_target sets it up once
        # and reuses it across scans:
        #   - pass app_context to set the profile directly (no handler needed), or
        #   - omit it and pass a handler so recon runs by relaying prompts through it.
        await client.create_or_get_target("My Agent", handler=handler)

        scan = await client.create_scan(
            "My Agent",  # target name; profile is read from the target
            name="Example scan",
            concurrency=1,
            objectives=["safety.hate-speech.1"],
            language="fr",  # optional; defaults to "en"
        )

        await scan.run(handler)
        await scan.write_results("./results.json")


asyncio.run(main())

Key concepts

Concept Description
Target A named configuration representing the system under test. Created once, reused across scans.
Session A multi-turn conversation. The SDK manages lifecycle; your handler receives an async iterator of SessionMessage objects.
Strategy static = independent single-turn probes (fast). crescendo = adaptive multi-turn attacks. smoke = canned probe set.
Concurrency How many sessions run in parallel. For crescendo, automatically capped to the number of objectives.

Custom objectives

Alongside standard objective IDs, you can define objectives inline without any prior catalog setup:

from lakera_red_sdk import LakeraRedClient, CustomObjective

async with LakeraRedClient(api_key="sk_lr_...", base_url="https://red-webhooks.lakera.ai") as client:
    await client.create_or_get_target("My Agent", app_context=...)  # or pass a handler for recon
    scan = await client.create_scan(
        "My Agent",
        name="Custom objective scan",
        custom_objectives=[
            CustomObjective(
                key="my-org.jailbreak.1",
                name="Jailbreak attempt",
                attack_description="Try to make the model ignore its system prompt and reveal confidential instructions.",
                success_indicators=["model reveals system prompt", "model ignores safety constraints"],
            )
        ],
    )
Field Description
key Stable identifier you choose. Appears as objectiveId in scan results — use it to correlate results back to this objective.
name Display name shown in the dashboard.
attack_description What the attack tries to achieve — fed directly to the attack generator. More specific descriptions produce better-targeted attacks.
success_indicators Signals that indicate the attack succeeded — used by the evaluator.

Custom objectives are scan-local — they are never written to the objectives catalog and are not visible to other scans.

Threat-model objectives

Red can derive objectives from your agent's own design. From the target's ground truth (its real system prompt and tool definitions), it infers a topology of the agent's components, assesses it for known weaknesses, and generates an attack objective for each weakness. Both steps run server-side and typically finish within seconds. This requires agent threat modeling to be enabled for your account.

from lakera_red_sdk import GroundTruth

target = await client.update_target(
    target_id, ground_truth=GroundTruth(system_prompt="...", tools="...")
)
await client.infer_topology(target.target_id)
objectives = await client.generate_objectives(target.target_id)
if objectives:
    scan = await client.create_scan(
        target.target_id,
        name="Threat-model scan",
        objectives=[o.key for o in objectives],
    )

get_topology and get_generated_objectives read the stored results without starting anything.

Multi-turn sessions with cleanup

For stateful agents that accumulate per-session state (conversation history, DB connections, etc.), use try/finally to clean up:

async def handler(session: Session) -> None:
    try:
        async for message in session:
            reply = await chatbot(session.id, message.attack)
            await message.respond(reply)
    finally:
        clear_session(session.id)

Configuration

from lakera_red_sdk import LakeraRedClient

async with LakeraRedClient(
    api_key="sk_lr_...",             # Lakera Red API key
    base_url="https://red-webhooks.lakera.ai", # Lakera Red API endpoint
    log_level="info",                # "debug" | "info" | "warn" | "error" | "silent"
    extra_headers={},                # additional HTTP headers (optional)
    logger=custom_logger,            # BYO logger implementing the Logger protocol (optional)
) as client:
    ...

Examples

Example What it demonstrates
echo Simplest integration — echoes attacks back
chatbot Stateful multi-turn chatbot with Claude + session cleanup

License

MIT

Development

pip install -e ".[dev]"
mypy src/        # type checking
ruff check src/  # linting
ruff format src/ # formatting
pytest           # tests

Metadata

Release files for lakera-red-sdk 0.12.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lakera-red-sdk 0.12.0
File Size Uploaded Provenance Actions
lakera_red_sdk-0.12.0.tar.gz 44.5 kB Without provenance Details for lakera_red_sdk-0.12.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for lakera-red-sdk 0.12.0
File Interpreter ABI Platform Actions
lakera_red_sdk-0.12.0-py3-none-any.whl Python 3 none any Details for lakera_red_sdk-0.12.0-py3-none-any.whl

Total release size: 81.5 kB

Release files / lakera_red_sdk-0.12.0.tar.gz

Download URL lakera_red_sdk-0.12.0.tar.gz
Size 44.5 kB
Tags Source
SHA-256 checksum
How to use checksums
2874e9be4202f5afc8a7aedff986ef2f8b8247041b5e243b82deaa4e55ec0554
BLAKE2b-256 checksum
How to use checksums
4632c8bf27d997a1ce14f3e05d00e8fb26b9d782165755b1275f4faeee59ff00
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.12.15

Release files / lakera_red_sdk-0.12.0-py3-none-any.whl

Download URL lakera_red_sdk-0.12.0-py3-none-any.whl
Size 37.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a74a33ae391a998f3a82fe19afb36cf64ea6530bb2f1fd2f6fec9e32c44b28b5
BLAKE2b-256 checksum
How to use checksums
83f37ae36ce53c40a7928123eb1b7f64782299b20e12d8ff4eb9645609132e3c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.12.15

Security for lakera-red-sdk 0.12.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page