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: create an API key, and give it to the SDK.
- Open a session: connect to Looper, and choose your project.
- Timeouts and retries: how long the SDK waits, what it retries for you, and what to do when starting something fails.
- Close the session: what happens when you close it, and what to finish first.
- When a session expires: how a session can end without being closed, and how to carry on.
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:
project: the project your work is kept under. A project is just a name you choose within your organization. It can be 1 to 128 characters long, using letters, digits,-,_or.. You don't need to create it first. Your runs belong to it, and so do your checkpoints' names. Sessions in the same project see the same checkpoints, and no others.display_name: a label for the session, so you can recognize it. It doesn't need to be unique.
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:
- The SDK warns you and keeps trying. When you try to open a session, it logs one warning that names the limit and how to free a slot, then retries for up to
retry_timeout_s. - If it still can't open one, it raises
CapacityExhausted(HTTP 429). It raises at once if you've turned retries off withretry_timeout_s=0. - To free a slot, close a session you don't need, or wait for one to expire. A session stops counting as soon as it's closed or expired, even while Looper is still cleaning up after it.
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:
CreationTimeout: itstimeout_sran out. The run or sampler may still be starting.CreationError: Looper refused it, or it failed while starting. Its.causeis Looper's error, such asInvalidArgumentfor a size the model doesn't offer. It'sretryableif that error is.
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:
- Its runs end and its samplers stop. Anything they haven't finished is cancelled, and their GPUs are freed soon after.
- Its checkpoints stay in the project, until they expire or you delete them.
- The session can't be used again. Every later call on
service, or on the clients it created, raisesSessionClosed.
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:
- To end one run, call
await training_client.delete(). - To stop one sampler, call
await sampling_client.stop().
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:
- Your program exits without closing the session.
- Your program loses its network connection.
- Your program blocks its event loop for most of a minute, for example with a long computation that isn't
asyncinside a coroutine. The heartbeats can't be sent while the loop is blocked. Run that kind of work withasyncio.to_thread.
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.