> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tensorlake.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Applications Quickstart

> Write, deploy, and call your first Tensorlake Application — a serverless agentic web-scraper with the Claude Agent SDK in under five minutes.

This guide will walk you through the process of writing, deploying, and calling Tensorlake Applications. You will learn how to build a serverless
agentic web-scrapper with Anthropic's Claude Agent SDK under 5 minutes.

Let's start with a simple "Hello, World!" application, to make sure your environment is set up correctly.

<Steps>
  <Step title="Install the Tensorlake SDK">
    ```bash theme={null}
    pip install tensorlake
    ```
  </Step>

  <Step title="Get an API Key">
    You can get an [API key](/platform/authentication#api-keys) from the Tensorlake Dashboard.

    ```bash theme={null}
    export TENSORLAKE_API_KEY=<your-api-key>
    ```
  </Step>

  <Step title="Create an application">
    Applications are defined by Python functions. Let's start with a template, that greets a user by name.

    ```bash theme={null}
    tl new hello_world
    ```

    This creates a file named `hello_world/hello_world.py` with the following content:

    ```python hello_world.py theme={null}
    from tensorlake.applications import application, function
    @application()
    @function()
    def greet(name: str) -> str:
        return f"Hello, {name}!"
    ```
  </Step>

  <Step title="Deploy It">
    Deploy your application referencing your application's source file.

    ```bash theme={null}
    tl deploy hello_world/hello_world.py
    ```
  </Step>
</Steps>

That's it — you now have a distributed app running in the cloud.

## Call Applications

Tensorlake gives you an HTTP endpoint, for calling your application remotely.

```
https://api.tensorlake.ai/applications/<app_name>
```

<Steps>
  <Step title="Get an API Key">
    Fetch a key from the [Tensorlake Dashboard](/platform/authentication#api-keys) and export it as an environment variable:

    ```bash theme={null}
    export TENSORLAKE_API_KEY=<API_KEY>
    ```
  </Step>

  <Step title="Make a request">
    <CodeGroup>
      ```bash bash theme={null}
      curl https://api.tensorlake.ai/applications/hello_world \
      -H "Authorization: Bearer $TENSORLAKE_API_KEY" \
      --json '"John"'
      # {"request_id":"beae8736ece31ef9"}
      ```

      ```python python theme={null}
      from tensorlake.applications import run_remote_application, Request

      request: Request = run_remote_application(greet, 'John')
      print(request.id)

      # "beae8736ece31ef9"
      ```
    </CodeGroup>

    This will return a request ID that you can use to track the progress of your request.
  </Step>

  <Step title="Check progress">
    Requests may run seconds to hours depending on your workload.

    <CodeGroup>
      ```bash bash theme={null}
      curl -X GET https://api.tensorlake.ai/applications/hello_world/requests/{request_id} \
      -H "Authorization: Bearer $TENSORLAKE_API_KEY"
      # {
      #   "id":"B0IwzHibTTfn5mCXHPGsu",
      #   "outcome":"success",
      #   "failure_reason":null,
      #   "request_error":null,
      #   .... other fields ...
      #}
      ```

      ```python python theme={null}
      # You don't need to poll for request completion. Retrieving the output will wait for the request to complete.
      ```
    </CodeGroup>

    The `outcome` field will be `success` or `failure` depending on whether the request completed successfully. It will be null if the request is still in progress.
  </Step>

  <Step title="Get the output">
    <CodeGroup>
      ```bash bash theme={null}
      curl -X GET https://api.tensorlake.ai/applications/hello_world/requests/{request_id}/output \
      -H "Authorization: Bearer $TENSORLAKE_API_KEY"
      # "Hello, John!"
      ```

      ```python python theme={null}
      from tensorlake.applications import run_remote_application, Request

      request: Request = run_remote_application(greet, 'John')
      output: str = request.output()

      print(output)
      # "Hello, John!"
      ```
    </CodeGroup>
  </Step>
</Steps>

## Testing Locally

Tensorlake Applications can run locally on your laptop. You can run them like regular python scripts.

```python hello_world.py theme={null}
# At the end of the file
from tensorlake.applications import run_local_application, Request
if __name__ == "__main__":
    request: Request = run_local_application(greet, 'John')
    output: str = request.output()
    print(output)
# "Hello, John!"
```

## Building an Agentic Code Interpreter

Now let's build a real agentic application. We will build a code interpreter agent with OpenAI Agent SDK.

The tensorlake application function will be the main agentic loop, and we will use a Tensorlake function to execute code,
and pass it as a tool to the agent. Whenever the agent needs to execute code, it will call the Tensorlake function and pass the code as a tool call.
The Tensorlake function will execute the code in an isolated container and return the output to the agent.

<Steps>
  <Step title="Add Your OpenAI API Key as a Secret">
    The agent needs access to the OpenAI API. Add your API key as a secret using the Tensorlake CLI:

    ```bash theme={null}
    tl secrets set OPENAI_API_KEY=<your-openai-api-key>
    ```

    This securely stores your API key so it can be injected into your application at runtime. The secret is referenced in the function decorator which uses the OpenAI Agent SDK and
    will be available as an environment variable.
  </Step>

  <Step title="Create the Application">
    ```python code_interpreter.py theme={null}
    import sys
    from io import StringIO
    from tensorlake.applications import application, function, Image

    # Image for the code execution container - has data science libraries
    code_exec_image = (
        Image(name="python:3.11-slim")
        .run("pip install numpy pandas matplotlib")
    )

    # Image for the agent container - has the OpenAI Agent SDK
    agent_image = (
        Image(name="python:3.11-slim")
        .run("pip install openai-agents")
    )

    @function(image=code_exec_image, cpu=2, memory=4)
    def execute_code(code: str) -> str:
        """Execute Python code in a secure sandbox and return the output."""
        stdout_capture = StringIO()
        old_stdout = sys.stdout
        
        try:
            sys.stdout = stdout_capture
            exec_globals = {"__builtins__": __builtins__}
            exec(code, exec_globals)
            sys.stdout = old_stdout
            return stdout_capture.getvalue()
        except Exception as e:
            sys.stdout = old_stdout
            return f"Error: {e}\nOutput: {stdout_capture.getvalue()}"

    @application()
    @function(image=agent_image, secrets=["OPENAI_API_KEY"])
    def code_interpreter_agent(user_request: str) -> str:
        """Run the agentic loop and return the final answer."""

        from agents import Agent, Runner, function_tool
        
        @function_tool
        def execute_python(code: str) -> str:
            """Execute Python code in a secure sandbox. Use this for calculations or data analysis."""
            return execute_code(code)
        
        agent = Agent(
            name="Code interpreter",
            model="gpt-4o",
            instructions="You are a helpful assistant that can execute Python code to solve problems.",
            tools=[execute_python],
        )
        
        result = Runner.run_sync(agent, user_request)
        return result.final_output
    ```
  </Step>

  <Step title="Deploy and Run">
    Deploy your application and call it:

    ```bash theme={null}
    tl deploy code_interpreter.py
    ```

    ```bash theme={null}
    curl https://api.tensorlake.ai/applications/code_interpreter_agent \
      -H "Authorization: Bearer $TENSORLAKE_API_KEY" \
      --json '"What is the square root of 273 * 312821 plus 1782?"'
    ```
  </Step>
</Steps>

<Callout icon="bulb" color="#FFC107" iconType="regular">
  On Lambda or Vercel, running arbitrary code execution would require complex sandboxing, security policies, and resource management — all in the same container as your main application.

  With Tensorlake, the `execute_code` function runs in a completely isolated container with its own CPU, memory, and dependencies. If code execution needs heavy compute or specialized libraries, it scales independently from your agent logic.

  You get secure, isolated code execution without managing infrastructure.
</Callout>

<Check>Tensorlake handles the infrastructure complexity so you can focus on building powerful AI tools.</Check>

## Next Steps

Here are some of the next things to learn about:

<Columns cols={2}>
  <Card title="Programming Guide" href="/applications/concepts">
    Learn key concepts and APIs to program applications.
  </Card>

  <Card title="Dependency management" href="/applications/images">
    Learn how to add dependencies for your applications.
  </Card>

  <Card title="Secrets" href="/applications/secrets">
    Learn how to manage secrets that your applications access.
  </Card>

  <Card title="Map-Reduce" href="/applications/map-reduce">
    Learn how to use map-reduce to process large datasets.
  </Card>

  <Card title="Building Workflows" href="/applications/building-workflows">
    Learn how to build multi-step workflows with parallel execution and optimized resource usage.
  </Card>

  <Card title="Futures" href="/applications/futures">
    Learn how to run multiple function calls in parallel using Futures.
  </Card>

  <Card title="Async Functions" href="/applications/async-functions">
    Learn how to use Python async/await with Tensorlake functions.
  </Card>
</Columns>
