Skip to main content

Deploying with HTTP Cloud Functions

HTTP Cloud Functions are the fastest way to deploy an Agent. They support auto-scaling and pay-as-you-go billing, making them ideal for lightweight Agents and low-frequency invocation scenarios.

Prerequisites​

  • A CloudBase environment has been activated
  • Node.js 18+ is installed
  • Agent development is complete

Quick Deployment​

1. Project Structure​

my-agent/
├── index.js # Entry file
├── agent.js # Agent logic
└── package.json # Dependency configuration

2. Agent Logic​

// agent.js
const { createAgent: createLangchainAgent } = require("langchain");
const { MemorySaver } = require("@langchain/langgraph");
const { ChatOpenAI } = require("@langchain/openai");
const { clientTools } = require("@cloudbase/agent-adapter-langchain");

const checkpointer = new MemorySaver();

function createLcAgent() {
// Use the built-in large model endpoint from CloudBase
const model = new ChatOpenAI({
model: process.env.TCB_AI_MODEL || "deepseek-v4-flash",
apiKey: process.env.TCB_API_KEY,
configuration: {
baseURL: `https://${process.env.TCB_ENV_ID}.api.tcloudbasegateway.com/v1/ai/cloudbase/v1`,
},
});

return createLangchainAgent({
model,
checkpointer,
middleware: [clientTools()],
});
}

module.exports = { createLcAgent };

3. Entry File​

// index.js
const { LangchainAgent } = require("@cloudbase/agent-adapter-langchain");
const { createExpressRoutes } = require("@cloudbase/agent-server");
const { createLcAgent } = require("./agent");
const express = require("express");

function createAgent() {
const lcAgent = createLcAgent();
return {
agent: new LangchainAgent({ agent: lcAgent }),
};
}

const app = express();
createExpressRoutes({ createAgent, express: app });

// Cloud function entry point
exports.main = app;

4. Dependency Configuration​

{
"name": "my-agent",
"version": "1.0.0",
"main": "index.js",
"dependencies": {
"@cloudbase/agent-adapter-langchain": "latest",
"@cloudbase/agent-server": "latest",
"langchain": "latest",
"@langchain/openai": "latest",
"@langchain/langgraph": "latest",
"express": "latest"
}
}

5. Deployment​

Method 1: Deploy with CLI​

Prerequisites: Install and log in to the CloudBase CLI

# Install CLI
npm install -g @cloudbase/cli

# Log in
tcb login

Deploy command:

# Run from the project directory
tcb fn deploy <functionName> --httpFn -e <env-id>

# Check deployment status
tcb fn list -e <env-id>
CLI Deployment Advantages
  • Fast deployment — no manual packaging or uploading required
  • Supports automated command-line deployment
  • Can be integrated into CI/CD pipelines
  • Detailed deployment logs and error messages

For more CLI commands and options, refer to the CloudBase CLI Documentation.

Method 2: Deploy via Console​

Upload a code package through the CloudBase console:

  1. Go to the CloudBase console
  2. Select Cloud Functions
  3. Click Create Cloud Function
  4. Select the HTTP Function type
  5. Upload the code package or edit the code online

Configuration Reference​

Environment Variables​

Set environment variables in the cloud function configuration:

VariableDescriptionExample
TCB_ENV_IDCloudBase environment IDyour-env-id
TCB_API_KEYCloudBase API keyak-xxx
TCB_AI_MODELModel namedeepseek-v4-flash
TCB_AI_PROVIDERModel provider (optional)cloudbase
tip

CloudBase has built-in support for Tencent Hunyuan and DeepSeek large models — no external API key required. For the list of supported models, refer to the Model Configuration Guide.

Timeout Configuration​

Agents typically require longer execution times. It is recommended to set a generous timeout:

  • Recommended timeout: 60–120 seconds
  • Maximum timeout: 900 seconds (15 minutes)

Memory Configuration​

Configure memory based on Agent complexity:

ScenarioRecommended Memory
Simple conversation256 MB
With tool calls512 MB
Complex workflows1024 MB+

Access Configuration​

After deployment, there are two ways to access the Agent service:

Create an AI Agent in the CloudBase console, associate it with the deployed cloud function, and obtain a unified access URL.

Steps:

  1. Go to the CloudBase console
  2. Select AI
  3. Click Create Agent
  4. Select Existing Service
  5. Choose the deployed HTTP cloud function
  6. Complete the creation

Access URL:

https://<env-id>.api.tcloudbasegateway.com/v1/aibot/bots/<agent-name>/send-message

Example:

curl 'https://<env-id>.api.tcloudbasegateway.com/v1/aibot/bots/<agent-name>/send-message' \
-H 'Content-Type: application/json' \
-H 'Accept: text/event-stream' \
--data-raw '{
"threadId": "550e8400-e29b-41d4-a716-446655440000",
"messages": [
{ "id": "msg-1", "role": "user", "content": "Hello" }
],
"tools": [],
"context": [],
"state": {},
"forwardedProps": {}
}'

Advantages:

  • ✅ Unified access URL format
  • ✅ Built-in authentication and access control
  • ✅ Supports Agent UI visual debugging
  • ✅ Automatic monitoring and logging integration

Method 2: Configure HTTP Access​

Directly configure an HTTP access path for the cloud function, suitable for custom access requirements.

Steps:

  1. Go to the CloudBase console
  2. Select Cloud Functions → HTTP Gateway
  3. Click Create
  4. Select the cloud function and configure the path
  5. Set the authentication method

Access URL:

https://<env-id>.<region>.app.tcloudbase.com/<path>

Example:

https://my-env-xxx.ap-shanghai.app.tcloudbase.com/agent

Authentication Configuration:

Authentication MethodDescriptionUse Case
No authenticationPublic accessTest environments
CloudBase authRequires login stateProduction environments
Security Note

It is strongly recommended to enable authentication in production environments to prevent API abuse. Method 1 (AI Agent integration) is recommended as it has a more comprehensive built-in security mechanism.

Local Debugging​

Before deploying, it is recommended to develop and debug locally. For detailed guidance, refer to:

👉 Local Development Guide

The local development guide covers:

  • Getting the project code (GitHub template / syncing from an online service)
  • Installing dependencies and configuring environment variables
  • Starting the local service
  • Debugging with cURL or a proxy
  • Troubleshooting common issues

Monitoring & Logging​

Viewing Logs​

View function logs through the CloudBase console:

  1. Go to the CloudBase console
  2. Select Cloud Functions → Logs
  3. Select the corresponding function to view its logs

Performance Monitoring​

View in the console:

  • Invocation count
  • Average duration
  • Error rate
  • Memory usage

FAQ​

1. Timeout Error​

Problem: Function execution times out

Solution:

  • Increase the timeout configuration
  • Optimize Agent logic to reduce unnecessary API calls
  • Consider deploying with CloudBase Run

2. Slow Cold Start​

Problem: Slow response on the first call

Solution:

  • Reduce the size of dependency packages
  • Use provisioned concurrency
  • Optimize initialization logic

3. Out of Memory​

Problem: OOM error

Solution:

  • Increase the memory configuration
  • Optimize memory usage
  • Avoid loading large models

Best Practices​

  1. Use environment variables: Configure sensitive information via environment variables
  2. Set a reasonable timeout: Set the timeout based on Agent complexity
  3. Enable authentication: Authentication must be enabled in production environments
  4. Monitor logs: Regularly review logs and performance metrics
  5. Error handling: Implement comprehensive error handling logic