Tensorlake SDK
repository·main·Indexed 21 days ago
https://github.com/tensorlakeai/tensorlakeA compute infrastructure platform for building agentic applications using high-performance, stateful Firecracker MicroVM sandboxes and serverless orchestration runtimes. It provides tools for running untrusted or LLM-generated code with features like memory and filesystem snapshots, sandbox pools for fast startup, and a versioned immutable filesystem via FilesystemClient. The SDK and tl CLI support deploying distributed orchestration APIs using @application and @function decorators.
What's inside tensorlake
- Tensorlake is a sandbox-native cloud designed for AI agents. It provides a compute platform for securely running untrusted or LLM-generated code in isolated Firecracker MicroVM sandboxes. It is optimized for heavy filesystem I/O, fast startup, and large-scale fan-out, supporting up to 5 million sandboxes per project.
What is the Function Executor?
mainThe Function Executor is a low-level process providing an API to load and run customer Functions. Each function execution is treated as a discrete task.
Key characteristics:
- Concurrency: Multiple tasks can be executed concurrently, with concurrency levels controlled by the API client.
- Resource Management: Because the Tensorlake SDK does not currently provide callbacks for customer code to manually free resources, the primary way to ensure all resources used by a function are released is to kill the Function Executor process.
- Lifecycle: Function Executors are managed by the
Executorcomponent (part of Indexify). They are created and destroyed automatically and are not intended to be deployed or managed manually by users.
Security considerations for Function Executor
mainWhen using or extending the Function Executor, adhere to the following threat model:
- Untrusted Code: Customer code running within the executor is assumed to be untrusted.
- Credential Isolation: The Function Executor must not obtain any credentials that grant access to resources not owned by the customer who owns the function.
How sandboxes work in Tensorlake
mainTensorlake sandboxes are isolated Firecracker MicroVMs that provide hardware-virtualized environments. Key features include:
- Snapshots: Capture both memory and filesystem state at any point to checkpoint an agent and resume from that exact state later.
- Auto Suspend/Resume: Sandboxes automatically suspend when idle and can resume in under a second without losing state.
- Fast Startup: Sandboxes are created in under a second via Lattice (a dynamic cluster scheduler). For even faster execution, use sandbox pools to maintain warm containers.
- Isolation: Provides a secure environment for running LLM-generated code or agent tools separate from your main infrastructure.
Compare `tl git mount` and `tl fs`
mainIt is important to distinguish between the Git-based workflow and the Filesystem-based workflow:
Feature tl git mounttl fsPrimary Purpose Private commit workflow for repositories Continuously published shared drive Versioning Named commits (snapshots) on a private line Path-addressed generations/snapshots Persistence snapshotcreates an immutable commitsnapshotrecords a permanent, billed generationWorkflow snapshot$\rightarrow$promoteto branchpushorsnapshotto save statetl fsSpecifics- Snapshots: Use
tl fs snapshot [PATH] -m "message"to record a permanent generation. Without-m, it is an ephemeral autosave. - History: Use
tl fs history [FILESYSTEM|PATH]to view permanent snapshots and the ephemeral WAL. The output separates these intosnapshotsandautosavesarrays. - Status:
tl fs status [PATH]reports local changes, last autosave time, and permanent snapshot count.
- Snapshots: Use
Use Sandbox Pools for fast startup
mainSandbox Pools allow you to pre-warm containers to achieve near-instant startup. You can define network policies (like
allow_internet_access) at the pool level. When a pool's network policy is updated, unclaimed warm containers are recycled, but containers already claimed by sandboxes keep their original policy.from tensorlake.sandbox import NetworkConfig # Create a pool with warm containers and no internet access pool = client.create_pool( image="tensorlake/ubuntu-minimal", warm_containers=3, network=NetworkConfig(allow_internet_access=False), ) # Claim a sandbox instantly from the pool resp = client.claim(pool.pool_id) sandbox = client.connect(resp.sandbox_id) # Named sandboxes can be reconnected later by name named = client.create(image="tensorlake/ubuntu-minimal", name="stable-name") sandbox = client.connect("stable-name")Install the `tl` CLI
mainThe
tlCLI is a standalone binary and is not distributed via PyPI or npm. Install it using the official installation script.curl -fsSL https://tensorlake.ai/install | shPerform structured data extraction from documents
mainYou can extract specific data from a document into a structured format by defining a Pydantic model and passing it to the
DocumentAI.parsemethod viaStructuredExtractionOptions.- Define a
BaseModelrepresenting your desired schema. - Initialize
DocumentAI. - Create
StructuredExtractionOptionsusing your schema. - Call
doc_ai.parse()with the file path and options. - Use
doc_ai.wait_for_completion(parse_id)to retrieve the results.
from tensorlake.documentai import DocumentAI from tensorlake.documentai.models import ParsingOptions, StructuredExtractionOptions, ChunkingStrategy from pydantic import BaseModel, Field from typing import List, Optional # 1. Define Schema class ResearchPaperSchema(BaseModel): title: str = Field(description="Title of the research paper") authors: List[str] = Field(description="List of author names") abstract: str = Field(description="Abstract of the paper") # 2. Initialize Client doc_ai = DocumentAI() file_path = "https://example.com/paper.pdf" # 3. Configure Options parsing_options = ParsingOptions(chunking_strategy=ChunkingStrategy.PAGE) structured_extraction_options = StructuredExtractionOptions( schema_name="Research Paper Analysis", json_schema=ResearchPaperSchema ) # 4. Parse parse_id = doc_ai.parse( file_path, parsing_options=parsing_options, structured_extraction_options=[structured_extraction_options], ) # 5. Wait for results result = doc_ai.wait_for_completion(parse_id)- Define a
Manage Git repository mounts with the Git workflow
mainWhen using
tl git mount, edits are first stored in a crash-safe local journal and continuously uploaded as ordered, idempotent checkpoints in a workspace WAL. These are not repository commits. To manage these changes, use the following workflow:Core Commands
tl git status [/code]: Check the status of the mount.tl git snapshot [/code] --message "<msg>": Flushes the current WAL state and materializes it as an immutable commit on the workspace's private line.tl git sync [/code] [BRANCH|TAG|FULL_COMMIT]: Refreshes the current source. If a target is provided, it switches the view or workspace. If the workspace has snapshots, userebaseinstead to change the base.tl git rebase [/code] BRANCH|TAG|FULL_COMMIT: Runs server-side to replay materialized snapshots and the unsnapshotted WAL tail onto a new base. Conflicts are shown asdiff3markers.tl git promote [/code] BRANCH [--merge]: The command to land changes. It autosaves edits, creates a snapshot, and lands it onto a real branch. By default, it performs a squash landing. Use--mergefor a true merge.tl git log [/code|REPOSITORY]andtl git smartlog [/code|REPOSITORY] [--project]: View history and retained recovery chains.
The
--publishWorkspaceA
--publishworkspace continuously serves a fixed target branch.- Snapshots: Explicit snapshots are reconciled directly onto the target branch.
- Restrictions:
synccan refresh the target but cannot retarget it.rebaseis rejected for--publishworkspaces. - Usage: Use a normal workspace if you need explicit rebase or retarget control.
tl git status [/code] tl git snapshot [/code] --message "checkpoint" tl git sync [/code] [BRANCH|TAG|FULL_COMMIT] tl git rebase [/code] BRANCH|TAG|FULL_COMMIT tl git promote [/code] BRANCH [--merge] tl git log [/code|REPOSITORY] tl git smartlog [/code|REPOSITORY] [--project]Install Tensorlake and OpenAI Agents SDK
mainTo build smart document understanding agents, install the required Python packages using pip:
pip install tensorlake openai-agentsEnsure you have set the following environment variables:
TENSORLAKE_API_KEY: Your Tensorlake authentication key.OPENAI_API_KEY: Your OpenAI API key.
!pip install tensorlake openai-agentsConfigure Tensorlake API Key and Login
mainAfter signing up at cloud.tensorlake.ai, export your API key as an environment variable and use the
tl logincommand to authenticate.export TENSORLAKE_API_KEY="your-api-key" tl loginDeploy and call Orchestration APIs
mainAfter defining your application, deploy it using the
tl deploycommand. Once deployed, you can invoke the application via HTTP requests. You can retrieve results using therequest_idor stream results using Server-Sent Events (SSE).# 1. Set secrets export TENSORLAKE_API_KEY="your-api-key" tl secrets set OPENAI_API_KEY "your-openai-key" # 2. Deploy tl deploy examples/readme_example/city_guide.py # 3. Invoke via HTTP curl https://api.tensorlake.ai/applications/city_guide_app \ -H "Authorization: Bearer $TENSORLAKE_API_KEY" \ --json '"San Francisco"' # 4. Get result using request_id curl https://api.tensorlake.ai/applications/city_guide_app/requests/{request_id}/output \ -H "Authorization: Bearer $TENSORLAKE_API_KEY" # 5. Stream results with SSE curl https://api.tensorlake.ai/applications/city_guide_app \ -H "Authorization: Bearer $TENSORLAKE_API_KEY" \ -H "Accept: text/event-stream" \ --json '"San Francisco"'