# How Team Memory Sharing Works in AgentMemory with TEAM_ID and USER_ID

> Learn how AgentMemory uses TEAM_ID and USER_ID for secure team memory sharing. Discover per-team KV namespaces and multi-user functions like team-share and team-feed.

- Repository: [Rohit Ghumare/agentmemory](https://github.com/rohitg00/agentmemory)
- Tags: how-to-guide
- Published: 2026-05-10

---

**AgentMemory isolates collaborative memories using `TEAM_ID` and `USER_ID` environment variables to create per-team KV namespaces, enabling multi-user sharing through the `mem::team-share`, `mem::team-feed`, and `mem::team-profile` functions.**

AgentMemory is an open-source memory layer for AI agents that supports collaborative workflows through team-based memory isolation. When configured with `TEAM_ID` and `USER_ID` environment variables, the system creates isolated namespaces where multiple users can share observations, memories, and patterns within a common team boundary. This article examines the implementation details based on the `rohitg00/agentmemory` source code.

## Configuring Team Mode with Environment Variables

Team functionality activates through environment-based configuration. The `loadTeamConfig()` function in [[`src/config.ts`](https://github.com/rohitg00/agentmemory/blob/main/src/config.ts)](https://github.com/rohitg00/agentmemory/blob/main/src/config.ts#L16-L23) reads `TEAM_ID`, `USER_ID`, and optional `TEAM_MODE` variables to instantiate a `TeamConfig` object.

When these variables are present, AgentMemory initializes internal team functions via `registerTeamFunction()`. The configuration propagates to all team-scoped operations, ensuring that every shared item carries the originating `userId` and belongs to the specified `teamId`.

## The Team KV Namespace and Key Structure

The data layer uses a hierarchical key pattern to isolate team data. According to [[`src/state/schema.ts`](https://github.com/rohitg00/agentmemory/blob/main/src/state/schema.ts)](https://github.com/rohitg00/agentmemory/blob/main/src/state/schema.ts#L20-L23), shared items reside under the namespace:

```

mem:team:{teamId}:shared

```

This convention ensures that team A cannot access team B's memories. The `KV.teamShared(teamId)` utility generates this key pattern, providing a consistent interface for the sharing functions.

## Sharing Memories Across Team Members

The core sharing mechanism lives in [[`src/functions/team.ts`](https://github.com/rohitg00/agentmemory/blob/main/src/functions/team.ts)](https://github.com/rohitg00/agentmemory/blob/main/src/functions/team.ts). When a client invokes `mem::team-share` (exposed as `POST /agentmemory/team/share`), the function executes the following workflow:

1. **Payload validation**: Validates `itemId`, `itemType` (memory or observation), optional `sessionId`, and `project`.
2. **Item retrieval**: If `itemType` is `"observation"`, fetches from `KV.observations(sessionId)`; otherwise retrieves from `KV.memories` (lines 45-48).
3. **Metadata wrapping**: Constructs a `TeamSharedItem` with:
   - `id`: Generated via `generateId("ts")`
   - `sharedBy`: The `userId` from `TeamConfig`
   - `sharedAt`: Current ISO timestamp
   - Original content, `project`, and `visibility` fields
4. **Storage**: Persists to `KV.teamShared(teamId)` (line 63).
5. **Audit**: Records the action via `recordAudit` with `teamId` and `userId` (lines 65-69).

```javascript
// Example: Sharing a memory via the internal function
const result = await memTeamShare({
  itemId: "mem_123",
  itemType: "memory",
  project: "agent-research",
  visibility: "shared"
});

```

## Retrieving Team Data: Feed and Profile

AgentMemory provides two distinct access patterns for shared team data.

### Accessing the Team Feed with mem::team-feed

The `mem::team-feed` function reads all items from `KV.teamShared(teamId)` (line 82) and returns a chronological list. It filters for items where `visibility === "shared"` and sorts by `sharedAt` descending (lines 84-89). The default limit is 20 items, configurable via the `limit` parameter.

```javascript
// Fetching recent team shares
const feed = await memTeamFeed({ limit: 10 });

```

### Generating Team Insights with mem::team-profile

The `mem::team-profile` function aggregates shared items into a unified `TeamProfile` (lines 96-141). It extracts:
- Distinct team members from `sharedBy` fields
- Top concepts and files referenced across memories
- Shared patterns inventory

The resulting profile stores at `KV.teamProfile(teamId)` (line 44) and provides a high-level view of collective agent knowledge.

## REST API Endpoints for Team Collaboration

The three internal functions map to HTTP endpoints in [[`src/triggers/api.ts`](https://github.com/rohitg00/agentmemory/blob/main/src/triggers/api.ts)](https://github.com/rohitg00/agentmemory/blob/main/src/triggers/api.ts#L1144-L1205):

- **POST /agentmemory/team/share** – Invokes `mem::team-share` to publish an item
- **GET /agentmemory/team/feed** – Invokes `mem::team-feed` with optional `limit` query parameter
- **GET /agentmemory/team/profile** – Invokes `mem::team-profile` to retrieve aggregated team data

```bash

# Example: Sharing a memory via REST API

curl -X POST /agentmemory/team/share \
  -H "Content-Type: application/json" \
  -d '{"itemId":"mem_123","itemType":"memory","project":"demo"}'

```

## Summary

- **Environment-based activation**: Set `TEAM_ID` and `USER_ID` to enable team mode via `loadTeamConfig()` in [`src/config.ts`](https://github.com/rohitg00/agentmemory/blob/main/src/config.ts).
- **Namespace isolation**: Team data resides under `mem:team:{teamId}:shared`, preventing cross-team leakage.
- **Three core functions**: `mem::team-share` writes items, `mem::team-feed` lists recent shares, and `mem::team-profile` generates aggregated insights.
- **Audit trail**: Every share operation records the `userId` and `teamId` for accountability.
- **REST exposure**: All functions are accessible via `/agentmemory/team/*` endpoints in [`src/triggers/api.ts`](https://github.com/rohitg00/agentmemory/blob/main/src/triggers/api.ts).

## Frequently Asked Questions

### What environment variables are required to enable team memory sharing in AgentMemory?

You must define `TEAM_ID` and `USER_ID` in your environment. Optionally set `TEAM_MODE` for additional configuration. The `loadTeamConfig()` function in [`src/config.ts`](https://github.com/rohitg00/agentmemory/blob/main/src/config.ts) validates these variables and constructs the runtime `TeamConfig` object that drives all team-scoped operations.

### How does AgentMemory isolate data between different teams?

AgentMemory uses a hierarchical KV key pattern `mem:team:{teamId}:shared` defined in [`src/state/schema.ts`](https://github.com/rohitg00/agentmemory/blob/main/src/state/schema.ts). Each team’s shared items live under their unique keyspace, and the `KV.teamShared(teamId)` accessor ensures queries only return data matching the configured `TEAM_ID`.

### What is the difference between mem::team-feed and mem::team-profile?

`mem::team-feed` returns a chronological list of individual shared items filtered by visibility, useful for displaying recent activity. `mem::team-profile` aggregates across all shared items to compute distinct members, top concepts, and file references, producing a summary useful for understanding collective agent knowledge.

### Can you share individual observations or only full memories with the team?

Both types are supported. When calling `mem::team-share`, set `itemType` to `"observation"` to share from `KV.observations(sessionId)`, or `"memory"` to share from `KV.memories`. The function handles retrieval and wrapping automatically based on this parameter.