Looper Documentation
Looper Cookbook

Session

A session is your program's connection to Looper. You open one at the start of your program, and everything you start through it, every training run and every sampler, belongs to it. When the session ends, they stop, and their GPUs are freed.

This page covers:

Authenticate

To use Looper, you need an API key. Create one under API Keys in the portal's left navigation. Then put it in the LOOPER_API_KEY environment variable, where the SDK reads it when you open a session:

export LOOPER_API_KEY=sk-...

You can also pass it as api_key= in ServiceClient.Config, for example if you load it from a secret store. Either way, the SDK sends the key with every request, and leaves it out when the config is printed, so it never shows up in your logs. Don't commit a key, or share a notebook that holds one.

Your organization needs credit. Without it, Looper refuses new work with a SpendRefused error. That includes opening a session, creating a run or a sampler (where it's the .cause of a CreationError), and each forward_backward, optim_step and sample. Work Looper already accepted still finishes.

The SDK connects to Looper at https://api.looper.goaly.ai, and you create keys in the portal at https://looper.goaly.ai.

If the key is missing or wrong, opening the session raises Unauthenticated (errors).

Open a session

To open a session, describe it with ServiceClient.Config, then call build(). build() connects to Looper and returns a ServiceClient. This guide calls it service, and you use it to start training runs and samplers.

from looper_sdk import ServiceClient

config = ServiceClient.Config(project="my-project", display_name="first-sft")
async with await config.build() as service:
    print("Session:", service.session.id)

The two settings here are:

ServiceClient.Config in the reference lists every setting.

async with closes the session when the block ends, even if your code raises an error. If the session needs to stay open across more than one block, as in a notebook or a server, keep it in a variable instead, with service = await config.build(), and call await service.close() when you're done.

How many sessions you can have open

By default, your organization can have 10 sessions open at once. That limit is shared across all your users, API keys and projects. To raise it, contact Looper. It counts only sessions, and one session can have many runs and samplers.

When your organization is at the limit:

Sessions that are already open keep working.

Timeouts and retries

Two settings in ServiceClient.Config control how long the SDK waits for Looper:

Setting Default What it does
request_timeout_s 60 How long the SDK waits for one request to Looper to answer. It must be more than 30 seconds, because Looper can hold a request for up to 30 seconds while it waits for a result.
retry_timeout_s 300 How long the SDK keeps resending a call that failed in a way that might work next time, before it gives up and raises the error. 0 sends each call only once.

The SDK retries for you. If a request gets no answer, or fails with an error marked retryable, such as Unavailable while Looper restarts, the SDK sends the same request again. Each call that sends Looper work to do, such as forward_backward, optim_step or sample, carries an idempotency key, which is a unique id for that call. A resent call keeps its key, so if Looper already accepted it, Looper returns that same operation instead of running it twice.

If a call still fails after the SDK's retries, you can resend it yourself. Use the same key, never a new one, so it can't run twice. Submit ahead shows how.

A timeout_s only ends your wait. When the timeout_s you pass to result, wait_checkpoint or wait_until_ready runs out, the call raises TimeoutError, but the work carries on in Looper.

When creating a run or a sampler fails

Creating a run or a sampler can fail in one of two ways:

Both errors carry the creation's key. If you call again with key=error.key in the same session, you get the run or sampler from the first try, never a second one.

Close the session

A session closes when you leave async with, or call await service.close(). Then:

So before you close, await the results you need, and wait for every checkpoint you want to keep with wait_checkpoint. If an upload fails, you can only save again while the run is still alive:

operation = await training_client.save_state("my-adapter")
checkpoint = await operation.result()
await service.wait_checkpoint(checkpoint.id)
await service.close()

Calls that are still running when you close get up to 60 seconds to finish, and the session stays alive meanwhile. After that, the SDK cancels them, and they raise SessionClosed.

close() itself waits up to 70 seconds for this. You can change that with close(timeout_s=...). If the wait runs out first, close() raises SessionCloseTimeout, and the cleanup carries on in the background. Call close() again to wait for it.

You don't have to close the whole session to free one run's or sampler's GPUs:

When a session expires

While your program runs, the SDK tells Looper every 10 seconds that it's still there. This is called a heartbeat. If Looper doesn't get a heartbeat for 60 seconds, the session expires.

That can happen when:

An expired session ends its runs and samplers, just as closing it does, and it can't come back, even if a late heartbeat arrives. After that, calls to its runs and checkpoint lookups through it raise SessionExpired, which isn't retryable. Creating a run or a sampler, and sampling, fail too.

To carry on, close the old service, open a new session, and create a new run. Checkpoints that finished uploading are still in your project, and a new sampler can load them, as Checkpoint explains.