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

# Google Cloud Run Function

> Deploy serverless AI processing with Google Cloud Run for scalable automation

## Overview

Execute Google Cloud Run functions from AgentFlow for serverless AI processing and automation. Build custom logic that scales automatically and integrates with Google Cloud services.

***

## Prerequisites

<CardGroup cols={2}>
  <Card title="Google Cloud Account" icon="cloud">
    Active GCP account with billing enabled
  </Card>

  <Card title="Cloud Run Access" icon="server">
    Permissions to deploy Cloud Run services
  </Card>

  <Card title="gcloud CLI" icon="terminal">
    Install and configure gcloud CLI
  </Card>

  <Card title="AgentFlow Admin" icon="shield">
    Admin access required
  </Card>
</CardGroup>

***

## Step 1: Setup Google Cloud Run Function

### Install Prerequisites

<Steps>
  <Step title="Install gcloud CLI">
    ```bash theme={null}
    # macOS
    brew install google-cloud-sdk

    # Linux
    curl https://sdk.cloud.google.com | bash

    # Windows
    # Download installer from cloud.google.com/sdk
    ```
  </Step>

  <Step title="Authenticate">
    ```bash theme={null}
    gcloud auth login
    gcloud config set project YOUR_PROJECT_ID
    ```
  </Step>

  <Step title="Enable APIs">
    ```bash theme={null}
    gcloud services enable run.googleapis.com
    gcloud services enable cloudbuild.googleapis.com
    ```
  </Step>
</Steps>

### Create Cloud Run Function

<Steps>
  <Step title="Initialize Project">
    ```bash theme={null}
    mkdir agentflow-processor
    cd agentflow-processor
    ```
  </Step>

  <Step title="Create main.py">
    ```python theme={null}
    import os
    import json
    from flask import Flask, request, jsonify
    from google.cloud import firestore
    import openai

    app = Flask(__name__)

    @app.route('/process', methods=['POST'])
    def process_message():
        try:
            # Get request data
            data = request.get_json()
            message = data.get('payload', {}).get('message_content')
            user_id = data.get('payload', {}).get('user_id')

            # Process with AI
            openai.api_key = os.environ.get('OPENAI_API_KEY')
            response = openai.ChatCompletion.create(
                model="gpt-4",
                messages=[
                    {"role": "system", "content": "You are a helpful assistant."},
                    {"role": "user", "content": message}
                ]
            )

            result = response.choices[0].message.content

            # Store in Firestore (optional)
            db = firestore.Client()
            db.collection('responses').add({
                'user_id': user_id,
                'message': message,
                'result': result,
                'timestamp': firestore.SERVER_TIMESTAMP
            })

            return jsonify({
                'data': {
                    'result': result,
                    'status': 'success',
                    'metadata': {
                        'user_id': user_id,
                        'processed_at': 'timestamp'
                    }
                }
            }), 200

        except Exception as e:
            return jsonify({
                'error': str(e),
                'status': 'failed'
            }), 500

    if __name__ == '__main__':
        port = int(os.environ.get('PORT', 8080))
        app.run(host='0.0.0.0', port=port)
    ```
  </Step>

  <Step title="Create requirements.txt">
    ```txt theme={null}
    Flask==3.0.0
    gunicorn==21.2.0
    openai==1.3.0
    google-cloud-firestore==2.13.1
    google-auth==2.23.4
    ```
  </Step>

  <Step title="Create Dockerfile">
    ```dockerfile theme={null}
    FROM python:3.11-slim

    WORKDIR /app

    COPY requirements.txt .
    RUN pip install --no-cache-dir -r requirements.txt

    COPY . .

    CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 main:app
    ```
  </Step>
</Steps>

### Deploy to Cloud Run

<Steps>
  <Step title="Build Container">
    ```bash theme={null}
    gcloud builds submit --tag gcr.io/YOUR_PROJECT_ID/agentflow-processor
    ```
  </Step>

  <Step title="Deploy Service">
    ```bash theme={null}
    gcloud run deploy agentflow-processor \
      --image gcr.io/YOUR_PROJECT_ID/agentflow-processor \
      --platform managed \
      --region us-central1 \
      --allow-unauthenticated \
      --set-env-vars OPENAI_API_KEY=your_api_key \
      --memory 1Gi \
      --cpu 1 \
      --max-instances 10 \
      --timeout 300
    ```
  </Step>

  <Step title="Get Service URL">
    ```bash theme={null}
    gcloud run services describe agentflow-processor \
      --platform managed \
      --region us-central1 \
      --format 'value(status.url)'
    ```

    Output: `https://agentflow-processor-xyz-uc.a.run.app`
  </Step>
</Steps>

***

## Step 2: Setup Authentication

### Create Service Account

```bash theme={null}
# Create service account
gcloud iam service-accounts create agentflow-invoker \
  --display-name "AgentFlow Cloud Run Invoker"

# Grant permissions
gcloud run services add-iam-policy-binding agentflow-processor \
  --member="serviceAccount:agentflow-invoker@YOUR_PROJECT_ID.iam.gserviceaccount.com" \
  --role="roles/run.invoker" \
  --region us-central1

# Create key
gcloud iam service-accounts keys create key.json \
  --iam-account agentflow-invoker@YOUR_PROJECT_ID.iam.gserviceaccount.com
```

### Generate Auth Token

```python theme={null}
from google.oauth2 import service_account
from google.auth.transport.requests import Request

# Load credentials
credentials = service_account.Credentials.from_service_account_file(
    'key.json',
    scopes=['https://www.googleapis.com/auth/cloud-platform']
)

# Get token
credentials.refresh(Request())
token = credentials.token
print(f"Bearer {token}")
```

***

## Step 3: Create AI Connection in AgentFlow

### Manual Configuration

1. **Admin Dashboard** → **AI Models** → **Add Model**

2. Basic Info:
   * **Name**: `Cloud Run AI Processor`
   * **Model ID**: `cloud-run-function-executor`
   * **Description**: `Serverless AI processing via Google Cloud Run`

3. API Settings:
   * **Endpoint**: `https://agentflow-processor-xyz-uc.a.run.app/process`
   * **Method**: POST

4. Headers:
   ```json theme={null}
   {
     "Authorization": "Bearer {{cloud_run_token}}",
     "Content-Type": "application/json",
     "User-Agent": "ChatPlatform/1.0"
   }
   ```

5. Request Schema:
   ```json theme={null}
   {
     "function_name": "agentflow-processor",
     "project_id": "YOUR_PROJECT_ID",
     "region": "us-central1",
     "endpoint_path": "/process",
     "payload": {
       "message": "{{message}}",
       "user_id": "{{user_id}}",
       "timestamp": "{{timestamp}}",
       "context": "{{context}}"
     },
     "execution_config": {
       "timeout": 300,
       "memory": "1Gi",
       "cpu": "1",
       "concurrency": 100,
       "max_instances": 10
     }
   }
   ```

6. Response Path: `data.result`

7. **Save**

***

## Step 4: Import via YAML

### YAML Configuration

Create `cloud-run-function-config.yaml`:

```yaml theme={null}
name: "Google Cloud Run Function"
model_id: "cloud-run-function-executor"
description: "Execute Google Cloud Run functions for serverless AI processing and automation"
endpoint: "https://{{function_name}}-{{project_id}}.{{region}}.run.app/{{endpoint_path}}"
method: "POST"

headers:
  Authorization: "Bearer {{cloud_run_token}}"
  Content-Type: "application/json"
  User-Agent: "ChatPlatform/1.0"

request_schema:
  function_name: "{{function_name}}"
  project_id: "{{project_id}}"
  region: "{{region}}"
  endpoint_path: "{{endpoint_path}}"
  payload:
    message: "{{message}}"
    user_id: "{{user_id}}"
    timestamp: "{{timestamp}}"
    context: "{{context}}"
  execution_config:
    timeout: 300
    memory: "1Gi"
    cpu: "1"
    concurrency: 100
    max_instances: 10

response_path: "data.result"

message_format:
  preset: "custom"
  mapping:
    role:
      source: "role"
      target: "payload.user_role"
      transform: "lowercase"
    content:
      source: "content"
      target: "payload.message_content"
      transform: "none"
    timestamp:
      source: "timestamp"
      target: "payload.timestamp"
      transform: "iso8601"
    context:
      source: "context"
      target: "payload.additional_context"
      transform: "json_stringify"
  customFields:
    - name: "cloud_run_metadata"
      value:
        platform: "google_cloud_run"
        version: "2.0"
        execution_type: "serverless"
        region: "{{region}}"
        project: "{{project_id}}"
      type: "object"
    - name: "function_configuration"
      value:
        timeout: 300
        memory: "1Gi"
        cpu: "1"
        concurrency: 100
        max_instances: 10
        min_instances: 0
      type: "object"

suggestion_prompts:
  - "Create a Cloud Run function for real-time image processing and analysis"
  - "Set up a function for automated document parsing and data extraction"
  - "Build a function for real-time language translation services"
  - "Create a function for automated email processing and routing"
  - "Set up a function for real-time data validation and transformation"
```

### Import Process

1. Update all `{{placeholder}}` values
2. **Admin Dashboard** → **AI Models** → **Import Model**
3. Upload YAML
4. Enter credentials
5. **Import**

***

## Step 5: Assign to Group

1. **Admin Dashboard** → **Groups**
2. Select group (e.g., "Development Team")
3. **Manage Models** → Enable **Cloud Run Function**
4. Configure:
   * **Execution Limit**: 10,000/day
   * **Timeout**: 5 minutes
   * **Priority**: High
5. **Save**

***

## Step 6: Use in Chat

### Trigger Functions

1. **Chat** → **New Conversation**
2. Select **Cloud Run AI Processor**
3. Send message

### Example Use Cases

<CodeGroup>
  ```text Image Analysis theme={null}
  Analyze this image URL for objects, text, and sentiment:
  https://example.com/image.jpg
  ```

  ```text Data Processing theme={null}
  Process this CSV data and generate summary statistics with visualizations
  [paste CSV data]
  ```

  ```text Translation theme={null}
  Translate this text to Spanish, French, and German:
  "Welcome to our platform. Please complete your profile."
  ```

  ```text Document Parsing theme={null}
  Extract structured data from this invoice:
  [upload or paste invoice content]
  ```
</CodeGroup>

***

## Advanced Function Examples

### Image Processing Function

```python theme={null}
import os
from flask import Flask, request, jsonify
from google.cloud import vision
from google.cloud import storage

app = Flask(__name__)

@app.route('/analyze-image', methods=['POST'])
def analyze_image():
    data = request.get_json()
    image_url = data.get('payload', {}).get('image_url')

    # Initialize Vision AI
    client = vision.ImageAnnotatorClient()

    # Analyze image
    image = vision.Image()
    image.source.image_uri = image_url

    response = client.annotate_image({
        'image': image,
        'features': [
            {'type_': vision.Feature.Type.LABEL_DETECTION},
            {'type_': vision.Feature.Type.TEXT_DETECTION},
            {'type_': vision.Feature.Type.FACE_DETECTION},
        ]
    })

    results = {
        'labels': [label.description for label in response.label_annotations],
        'text': response.text_annotations[0].description if response.text_annotations else '',
        'faces': len(response.face_annotations)
    }

    return jsonify({'data': {'result': results}})
```

### Data Processing Function

```python theme={null}
import pandas as pd
import json
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/process-data', methods=['POST'])
def process_data():
    data = request.get_json()
    csv_data = data.get('payload', {}).get('csv_data')

    # Parse CSV
    df = pd.read_csv(io.StringIO(csv_data))

    # Generate statistics
    stats = {
        'summary': df.describe().to_dict(),
        'correlations': df.corr().to_dict(),
        'missing_values': df.isnull().sum().to_dict(),
        'data_types': df.dtypes.astype(str).to_dict()
    }

    return jsonify({'data': {'result': stats}})
```

### Translation Function

```python theme={null}
from google.cloud import translate_v2 as translate

app = Flask(__name__)

@app.route('/translate', methods=['POST'])
def translate_text():
    data = request.get_json()
    text = data.get('payload', {}).get('text')
    target_languages = data.get('payload', {}).get('languages', ['es', 'fr', 'de'])

    # Initialize translator
    client = translate.Client()

    # Translate to multiple languages
    translations = {}
    for lang in target_languages:
        result = client.translate(text, target_language=lang)
        translations[lang] = result['translatedText']

    return jsonify({'data': {'result': translations}})
```

***

## Scaling & Performance

### Auto-Scaling Configuration

```bash theme={null}
gcloud run services update agentflow-processor \
  --min-instances 0 \
  --max-instances 100 \
  --concurrency 80 \
  --cpu-throttling \
  --region us-central1
```

### Performance Optimization

<Steps>
  <Step title="Cold Start Reduction">
    * Use minimum instances (min-instances=1)
    * Optimize container size
    * Implement connection pooling
  </Step>

  <Step title="Memory Optimization">
    ```bash theme={null}
    # Adjust based on workload
    --memory 512Mi   # Light processing
    --memory 2Gi     # Heavy processing
    --memory 4Gi     # ML inference
    ```
  </Step>

  <Step title="CPU Allocation">
    ```bash theme={null}
    --cpu 1      # Standard
    --cpu 2      # CPU-intensive
    --cpu 4      # Maximum performance
    ```
  </Step>

  <Step title="Concurrency Tuning">
    ```bash theme={null}
    # Requests per instance
    --concurrency 80   # I/O bound
    --concurrency 1    # CPU bound
    ```
  </Step>
</Steps>

### Regional Deployment

Deploy to multiple regions:

```bash theme={null}
# Deploy to us-central1
gcloud run deploy agentflow-processor \
  --image gcr.io/PROJECT_ID/agentflow-processor \
  --region us-central1

# Deploy to europe-west1
gcloud run deploy agentflow-processor \
  --image gcr.io/PROJECT_ID/agentflow-processor \
  --region europe-west1

# Deploy to asia-east1
gcloud run deploy agentflow-processor \
  --image gcr.io/PROJECT_ID/agentflow-processor \
  --region asia-east1
```

***

## Monitoring & Logging

### Cloud Logging

View logs:

```bash theme={null}
# Stream logs
gcloud run services logs tail agentflow-processor \
  --region us-central1

# Filter by severity
gcloud logging read "resource.type=cloud_run_revision AND severity>=ERROR" \
  --limit 50
```

### Cloud Monitoring

Setup alerts:

```bash theme={null}
# Create alert policy
gcloud alpha monitoring policies create \
  --notification-channels=CHANNEL_ID \
  --display-name="Cloud Run High Error Rate" \
  --condition-display-name="Error rate > 5%" \
  --condition-threshold-value=5 \
  --condition-threshold-duration=60s
```

### Custom Metrics

```python theme={null}
from google.cloud import monitoring_v3

def write_custom_metric(project_id, value):
    client = monitoring_v3.MetricServiceClient()
    project_name = f"projects/{project_id}"

    series = monitoring_v3.TimeSeries()
    series.metric.type = "custom.googleapis.com/agentflow/processing_time"
    series.resource.type = "cloud_run_revision"

    point = series.points.add()
    point.value.double_value = value
    point.interval.end_time.seconds = int(time.time())

    client.create_time_series(request={"name": project_name, "time_series": [series]})
```

***

## Security Best Practices

<CardGroup cols={2}>
  <Card title="Authentication" icon="lock">
    Always require authentication for sensitive endpoints
  </Card>

  <Card title="Service Accounts" icon="user-shield">
    Use least-privilege service accounts
  </Card>

  <Card title="Secrets Management" icon="key">
    Store secrets in Secret Manager, not environment variables
  </Card>

  <Card title="Network Security" icon="shield">
    Use VPC connectors for private resources
  </Card>

  <Card title="Input Validation" icon="check">
    Validate and sanitize all inputs
  </Card>

  <Card title="Audit Logging" icon="file-text">
    Enable Cloud Audit Logs
  </Card>
</CardGroup>

### Using Secret Manager

```python theme={null}
from google.cloud import secretmanager

def access_secret(project_id, secret_id):
    client = secretmanager.SecretManagerServiceClient()
    name = f"projects/{project_id}/secrets/{secret_id}/versions/latest"
    response = client.access_secret_version(request={"name": name})
    return response.payload.data.decode("UTF-8")

# Usage
openai_key = access_secret("my-project", "openai-api-key")
```

***

## Cost Optimization

### Pricing Breakdown

| Resource   | Cost                     |
| ---------- | ------------------------ |
| CPU        | \$0.00002400/vCPU-second |
| Memory     | \$0.00000250/GiB-second  |
| Requests   | \$0.40/million requests  |
| Networking | \$0.12/GB egress         |

### Optimization Strategies

<Steps>
  <Step title="Right-Size Resources">
    Match CPU/memory to actual needs
  </Step>

  <Step title="Minimize Cold Starts">
    Use min-instances only when necessary
  </Step>

  <Step title="Optimize Container">
    Reduce image size and dependencies
  </Step>

  <Step title="Cache Responses">
    Implement caching for repeated queries
  </Step>

  <Step title="Batch Processing">
    Process multiple items per request
  </Step>
</Steps>

***

## Troubleshooting

<AccordionGroup>
  <Accordion title="503 Service Unavailable">
    **Causes**:

    * Cold start timeout
    * Resource limits exceeded
    * Deployment issues

    **Fix**:

    * Increase timeout
    * Add min-instances
    * Check deployment logs
  </Accordion>

  <Accordion title="429 Too Many Requests">
    **Cause**: Rate limit exceeded

    **Fix**:

    * Increase max-instances
    * Implement request queuing
    * Distribute across regions
  </Accordion>

  <Accordion title="Memory Exceeded">
    **Symptoms**: OOM errors in logs

    **Fix**:

    * Increase memory allocation
    * Optimize code for memory efficiency
    * Implement streaming for large data
  </Accordion>

  <Accordion title="Authentication Failed">
    **Check**:

    * Service account permissions
    * Token validity
    * IAM policies

    **Fix**: Regenerate credentials
  </Accordion>
</AccordionGroup>

***

## CI/CD Integration

### GitHub Actions Deployment

```yaml theme={null}
name: Deploy to Cloud Run

on:
  push:
    branches: [ main ]

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Setup Cloud SDK
      uses: google-github-actions/setup-gcloud@v0
      with:
        service_account_key: ${{ secrets.GCP_SA_KEY }}
        project_id: ${{ secrets.GCP_PROJECT_ID }}

    - name: Build and Deploy
      run: |
        gcloud builds submit --tag gcr.io/${{ secrets.GCP_PROJECT_ID }}/agentflow-processor
        gcloud run deploy agentflow-processor \
          --image gcr.io/${{ secrets.GCP_PROJECT_ID }}/agentflow-processor \
          --platform managed \
          --region us-central1 \
          --allow-unauthenticated
```

***

## Next Steps

<CardGroup cols={2}>
  <Card title="LangChain Agents" icon="link" href="/examples/langchain-agent">
    Build AI agents with tools
  </Card>

  <Card title="Workflow Automation" icon="diagram-project" href="/examples/n8n-workflow">
    Combine with n8n workflows
  </Card>

  <Card title="OpenAI Assistants" icon="robot" href="/examples/openai-agent-builder">
    Advanced AI agents
  </Card>

  <Card title="Analytics" icon="chart-bar" href="/analytics/usage-metrics">
    Monitor function performance
  </Card>
</CardGroup>
