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

# Geavanceerde functies

Dit document behandelt geavanceerde functies en best practices voor Novita Agent Runtime.

***

## Inhoudsopgave

* [Referentie voor configuratiebestand](#configuration-file-reference)
* [Beheer van omgevingsvariabelen](#environment-variables-management)
* [Streaming-antwoorden](#streaming-responses)
* [Versiebeheer](#version-management)
* [Statuscontroles](#health-checks)
* [Multi-turn gesprekken](#multi-turn-conversations)
* [Voorbeeldprojecten](#example-projects)

***

## Referentie voor configuratiebestand

### .novita-agent.yaml-structuur

De `.novita-agent.yaml` configuratiebestand gebruikt YAML-indeling in Kubernetes-stijl:

```yaml theme={"system"}
apiVersion: v1
kind: Agent
metadata:
  name: my-agent              # Agent name (must consist of lowercase letters, numbers, and hyphens only)
  version: 1.0.0              # Agent version (semantic versioning)
  author: dev@example.com     # Author email (required)
  description: My AI Agent    # Agent description (optional)
  created: '2025-10-23T10:30:00Z'  # Creation time (auto-generated)

spec:
  entrypoint: app.py          # Python entry file (must be .py file)
  
  # Environment variables configuration (optional)
  envVars:
    MODEL_NAME: deepseek/deepseek-v3-0324
    TEMPERATURE: '0.7'
  
  # Runtime configuration (optional, applied to the built sandbox template)
  runtime:
    timeout: 300              # Startup timeout in seconds (1-3600, default 300)
    memory_limit: 1Gi         # Memory limit (supports "512Mi", "1Gi", etc.)
    cpu_limit: '1'            # CPU limit (supports "1", "1000m", etc.)

# Status field (maintained by the system, should not be modified manually by users)
status:
  phase: deployed             # Current phase: pending/building/deployed/failed
  agent_id: agent-xxxxx       # Agent ID (auto-generated after deployment)
  last_deployed: '2025-10-23T10:35:00Z'  # Last deployment time
  build_id: build_xyz789      # Build ID (auto-generated after deployment)
```

### Configuratie wijzigen

#### CPU- en geheugeninstellingen wijzigen

Wijzig de resourceconfiguratie onder `spec.runtime` in `.novita-agent.yaml`:

```yaml theme={"system"}
spec:
  runtime:
    # CPU configuration
    cpu_limit: '2'        # 2 CPU cores
    # Memory configuration
    memory_limit: 2Gi     # 2 GB memory
```

#### Omgevingsvariabelen wijzigen

De `spec.envVars` in `.novita-agent.yaml` wordt alleen gebruikt voor de CLI's `agent invoke` opdracht en wordt niet doorgegeven aan de gedeployde sandbox-template.

Wijzig omgevingsvariabelen onder `spec.envVars` in `.novita-agent.yaml`:

```yaml theme={"system"}
spec:
  envVars:
    # LLM configuration
    MODEL_NAME: deepseek/deepseek-v3-0324
    TEMPERATURE: '0.7'
```

**Opmerking**:

* ⚠️ **Sla geen gevoelige informatie op** (zoals API-sleutels) in `.novita-agent.yaml`
* Je kunt ook omgevingsvariabelen doorgeven via de `--env` parameter bij het uitvoeren van de `agent invoke` command

#### Opnieuw implementeren om configuratiewijzigingen toe te passen

Na het wijzigen van resourcespecificaties in `.novita-agent.yaml`, is opnieuw uitrollen vereist:

```bash theme={"system"}
# Redeploy (creates a new version)
npx novita-sandbox-cli agent launch
```

***

## Beheer van omgevingsvariabelen

Er zijn verschillende manieren om omgevingsvariabelen door te geven aan Agents die in sandbox-instanties draaien:

### Methode 1: Definiëren in configuratiebestand (alleen CLI-aanroep)

Definieer omgevingsvariabelen onder `spec.envVars` in `.novita-agent.yaml`:

```yaml theme={"system"}
spec:
  envVars:
    MODEL_NAME: deepseek/deepseek-v3-0324
    TEMPERATURE: '0.7'
```

### Methode 2: Dynamisch doorgeven via SDK

Wanneer je een Agent aanroept met behulp van de SDK's `invoke_agent_runtime` methode, geef ze dynamisch door via de `envVars` parameter:

```python theme={"system"}
import os
from novita_sandbox.agent_runtime import AgentRuntimeClient

client = AgentRuntimeClient(api_key=os.getenv("NOVITA_API_KEY"))

response = await client.invoke_agent_runtime(
    agentId="agent-xxxxx",
    payload=payload,
    envVars={
        # Read sensitive information from environment variables
        "NOVITA_API_KEY": os.getenv("NOVITA_API_KEY"),
        "DATABASE_PASSWORD": os.getenv("DATABASE_PASSWORD"),
        
        # Or pass directly
        "MODEL_NAME": "deepseek/deepseek-v3-0324",
        "TEMPERATURE": "0.7"
    }
)
```

***

## Streaming-antwoorden

### Streaming implementeren met synchrone generators

Gebruik Python-generators om streaming-antwoorden te implementeren:

```python theme={"system"}
from novita_sandbox.agent_runtime import AgentRuntimeApp

app = AgentRuntimeApp()

@app.entrypoint
def streaming_agent(request: dict):
    """Synchronous streaming response"""
    prompt = request.get("prompt", "")

    # Use generator to return chunks
    for i, chunk in enumerate(generate_response(prompt)):
        yield {
            "chunk": chunk,
            "type": "content",
            "index": i
        }
    # Send end marker
    yield {"chunk": "", "type": "end"}

```

### Streaming implementeren met async generators

Gebruik Python async generators:

```python theme={"system"}
import asyncio

@app.entrypoint
async def async_streaming_agent(request: dict):
    """Async streaming response"""
    prompt = request.get("prompt", "")

    async for chunk in async_generate_response(prompt):
        yield {
            "chunk": chunk,
            "type": "content"
        }
    yield {"chunk": "", "type": "end"}

```

### Voorbeeld van een LangChain streaming-respons

Compleet voorbeeld met LangChain voor streaming-responses:

```python theme={"system"}
import os
from langchain_openai import ChatOpenAI
from langchain.callbacks.base import BaseCallbackHandler
from novita_sandbox.agent_runtime import AgentRuntimeApp

app = AgentRuntimeApp()

class StreamingHandler(BaseCallbackHandler):
    """Streaming callback handler"""
    def __init__(self):
        self.tokens = []
    
    def on_llm_new_token(self, token: str, **kwargs):
        self.tokens.append(token)

@app.entrypoint
def langchain_streaming_agent(request: dict):
    """LangChain streaming response"""
    prompt = request.get("prompt", "")

    # Create streaming-enabled LLM
    llm = ChatOpenAI(
        api_key=os.getenv("NOVITA_API_KEY"),
        streaming=True
    )
    
    # Stream invocation
    for chunk in llm.stream(prompt):
        if chunk.content:
            yield {
                "chunk": chunk.content,
                "type": "content"
            }
    yield {"chunk": "", "type": "end"}
```

### Een streaming Agent aanroepen

Roep een streaming Agent aan met behulp van de SDK:

```python theme={"system"}
import asyncio
import json
import os
from novita_sandbox.agent_runtime import AgentRuntimeClient

async def call_streaming_agent():
    client = AgentRuntimeClient(api_key=os.getenv("NOVITA_API_KEY"))
    
    payload = json.dumps({
        "prompt": "Tell me a story"
    }).encode()
    
    response = await client.invoke_agent_runtime(
        agentId="agent-xxxxx",
        payload=payload
    )
    
    # Process streaming response
    print("Streaming response:")
    print(response)
```

***

## Versiebeheer

### Een nieuwe agentversie implementeren

Wijzig het versienummer en implementeer een nieuwe versie:

```bash theme={"system"}
# Modify version number
npx novita-sandbox-cli agent configure --agent-version 1.1.0

# Deploy new version
npx novita-sandbox-cli agent launch
```

Na succesvolle implementatie wordt een nieuwe `agent_id` wordt gegenereerd. Elke implementatie genereert een unieke `agent_id` die overeenkomt met een specifieke versie.

***

## Gezondheidscontroles

### Standaard gezondheidscontrole-eindpunt

AgentRuntimeApp biedt automatisch een `/ping` healthcheck-eindpunt:

```python theme={"system"}
from novita_sandbox.agent_runtime import AgentRuntimeApp

app = AgentRuntimeApp()

# Default health check automatically responds with {"status": "Healthy"}
```

### Aangepaste healthchecks

Gebruik de `@app.ping` decorator om de logica voor gezondheidscontroles aan te passen:

```python theme={"system"}
@app.ping
def custom_health_check():
    """Custom health check"""
    # Check dependent services
    db_ok = check_database_connection()
    llm_ok = check_llm_service()
    
    if db_ok and llm_ok:
        return {"status": "Healthy"}
    elif db_ok or llm_ok:
        return {"status": "HealthyBusy"}  # Partially available
    else:
        return {"status": "Unhealthy"}  # Unavailable

def check_database_connection():
    """Check database connection"""
    try:
        # Simulate database check
        return True
    except:
        return False

def check_llm_service():
    """Check LLM service"""
    try:
        # Simulate LLM service check
        return True
    except:
        return False
```

### Ondersteunde healthcheck-statussen

Agents kunnen de volgende health-statussen retourneren:

\| Status | Beschrijving | HTTP-statuscode |
\|--------|-------------|------------------| `Healthy` | Agent is volledig beschikbaar | 200 | `HealthyBusy` | Agent is gedeeltelijk beschikbaar (bijv. verwerkt zware belasting) | 200 | `Unhealthy` | Agent is niet beschikbaar | 503 |

***

## Gesprekken met meerdere beurten

### Sessie-ID gebruiken voor gesprekken met meerdere beurten

Gebruik de `runtimeSessionId` parameter om meerdere verzoeken naar dezelfde sandboxinstantie te routeren:

```python theme={"system"}
import uuid
import json
import os
from novita_sandbox.agent_runtime import AgentRuntimeClient

async def multi_turn_conversation():
    runtime_session_id = str(uuid.uuid4())
    client = AgentRuntimeClient(api_key=os.getenv("NOVITA_API_KEY"))
    agent_id = "agent-xxxxx"
    
    # First turn
    response1 = await client.invoke_agent_runtime(
        agentId=agent_id,
        payload=json.dumps({"prompt": "Hello, my name is John"}).encode(),
        runtimeSessionId=runtime_session_id,
    )
    print(f"AI: {response1}")
    
    # Second turn (sent to the same sandbox instance, Agent remembers the context)
    response2 = await client.invoke_agent_runtime(
        agentId=agent_id,
        payload=json.dumps({"prompt": "What's my name?"}).encode(),
        runtimeSessionId=runtime_session_id,
    )
    print(f"AI: {response2}")  # Should answer "John"
```

***

## Voorbeeldprojecten

We bieden een compleet voorbeeldproject op basis van LangGraph, dat laat zien hoe je echte AI-applicaties bouwt met Novita Agent Runtime.

### Projectrepository

🔗 [https://github.com/novitalabs/Novita-CollabHub/tree/main/examples/agent-runtime/agentic-frameworks/langgraph](https://github.com/novitalabs/Novita-CollabHub/tree/main/examples/agent-runtime/agentic-frameworks/langgraph)
