Create an Agent Platform resource for Sessions and Memory Bank

Copy as Markdown

An ADK agent running on Cloud Run, GKE, or another environment can use Agent Platform for managed Sessions and Memory Bank. Before configuring either service, create the backing Agent Engine resource. The API identifies this resource as a reasoningEngine.

This post creates the resource through the REST API, waits for the asynchronous operation to finish, and prints the two service URIs needed by an ADK deployment.

Note: The public Memory Bank setup guide shows the equivalent Vertex AI SDK flow and covers project setup, API enablement, and supported regions.

Create the resource

Before running the script, select your Google Cloud project with gcloud config set project PROJECT_ID and make sure jq is installed. Update LOCATION if you want to use a region other than us-west1.

The companion repository contains this script:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# 1. Set your variables
PROJECT_ID=$(gcloud config get-value project)
LOCATION="us-west1" # Replace with your target region
RESOURCE_DISPLAY_NAME="Agent Backend Resource"

# 2. Build the request payload
SETTINGS="{
    \"displayName\": \"${RESOURCE_DISPLAY_NAME}\"
  }"

# 3. Execute the REST API request, capturing the response body
RESPONSE=$(curl -s -X POST \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  -H "Content-Type: application/json" \
  "https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/reasoningEngines" \
  -d "${SETTINGS}")

# 4. Check for errors in the response
if [ -z "$RESPONSE" ]; then
  echo "Error: empty response from API" >&2
  exit 1
fi

if echo "$RESPONSE" | grep -q '"error"'; then
  echo "Error: API request failed" >&2
  echo "$RESPONSE" >&2
  exit 1
fi

# 5. Parse the "name" field from the response
NAME=$(echo "$RESPONSE" | jq -r '.name')

if [ -z "$NAME" ] || [ "$NAME" = "null" ]; then
  echo "Error: could not parse 'name' from response" >&2
  echo "$RESPONSE" >&2
  exit 1
fi

echo "Operation name: $NAME"

# 6. Derive the engine resource name from the operation name
ENGINE_NAME="${NAME%/operations/*}"

# 6b. Wait for the create operation to complete (engine is provisioned async)
echo "Waiting for engine to be created..."
until [ "$(curl -s \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  "https://${LOCATION}-aiplatform.googleapis.com/v1/${NAME}" | jq -r '.done')" = "true" ]; do
  sleep 5
done
echo "Engine ready: $ENGINE_NAME"

echo
echo "Add these env variables to your deployment:"
echo "SESSION_SERVICE_URI=agentengine://${ENGINE_NAME}"
echo "MEMORY_SERVICE_URI=agentengine://${ENGINE_NAME}"
echo

# 7. Create a memory under the engine
echo "Creating a test memory"
MEMORY="{
    \"fact\": \"My first memory.\",
    \"scope\": {\"placeholder\": \"true\"}
  }"
curl -s -X POST \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  -H "Content-Type: application/json" \
  "https://${LOCATION}-aiplatform.googleapis.com/v1/${ENGINE_NAME}/memories" \
  -d "${MEMORY}"

# 8. Create a session under the engine
echo "Creating a test session"
SESSION="{
    \"userId\": \"example-user\"
  }"
curl -s -X POST \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  -H "Content-Type: application/json" \
  "https://${LOCATION}-aiplatform.googleapis.com/v1/${ENGINE_NAME}/sessions" \
  -d "${SESSION}"

deploy_backend.sh

The create request returns a long-running operation. The script derives the resource name from that operation, waits until provisioning completes, and prints values in this format:

SESSION_SERVICE_URI=agentengine://projects/PROJECT/locations/REGION/reasoningEngines/ENGINE_ID
MEMORY_SERVICE_URI=agentengine://projects/PROJECT/locations/REGION/reasoningEngines/ENGINE_ID

The final two requests create a test memory and session. Their responses should identify resources under the new reasoningEngine. The example scope and user ID are placeholders rather than a recommended production data model.

With the resource available, configure an agent on Cloud Run or GKE to use Sessions and Memory Bank.