MCPInstall

cosmosdb-best-practices

Azure Cosmos DB performance optimization and best practices guidelines for NoSQL, partitioning, queries, and SDK usage. Use when writing, reviewing, or refactoring code that interacts with Azure Cosmos DB, designing data models, optimizing queries, or implementing high-performance database operations. USE FOR: Cosmos DB NoSQL, partition key design, RU optimization, point reads, cross-partition queries, SDK singleton, CosmosClient, container modeling, change feed, bulk operations, vector search, full-text search, hierarchical partition keys, global distribution, autoscale throughput, indexing policy, DO NOT USE FOR: PostgreSQL, MySQL, MongoDB (non-Azure), DynamoDB, Cassandra, Azure SQL, Cosmos DB for PostgreSQL (vCore), Cosmos DB for MongoDB vCore, Azure DocumentDB, general SQL databases, Redis, Elasticsearch.

Installs
1.1K
Last 24h
—
Trending rank
—
Security
Not audited

Install

Works with every agent the skills CLI supports.

npx skills add https://github.com/azurecosmosdb/cosmosdb-agent-kit --skill cosmosdb-best-practices
  1. 01

    Install

    Run the command in your project. The CLI asks which agents to install for.

  2. 02

    Describe the task

    Ask your agent for something this skill covers. It loads cosmosdb-best-practices automatically when relevant.

  3. 03

    Review the result

    The agent follows the SKILL.md steps, scripts and references bundled with the skill.

SKILL.md

The instructions your agent reads when the skill is triggered.

Azure Cosmos DB Best Practices

Comprehensive performance optimization guide for Azure Cosmos DB applications, containing 100+ rules across 13 categories, prioritized by impact to guide automated refactoring and code generation.

Before answering, read the linked rule files relevant to the user's question. The quick reference below is an index, not the full guidance: SDK APIs, limits, configuration prerequisites, and caveats live in the rule bodies. Use the language-specific integration rule when one exists rather than substituting a generic pattern. Keep answers concise, but include the constraints that make the recommendation correct for the user's scenario.

When to Apply

Reference these guidelines when:

  • Designing data models for Cosmos DB
  • Choosing partition keys
  • Writing or optimizing queries
  • Implementing SDK patterns
  • Using the Cosmos DB Emulator for local development
  • Inspecting or managing Cosmos DB data with developer tooling
  • Implementing vector search or RAG features on Cosmos DB
  • Reviewing code for performance issues
  • Configuring throughput and scaling
  • Building globally distributed applications

Rule Categories by Priority

PriorityCategoryImpactPrefix
1Data ModelingCRITICALmodel-
2Partition Key DesignCRITICALpartition-
3Query OptimizationHIGHquery-
4SDK Best PracticesHIGHsdk-
5Indexing StrategiesMEDIUM-HIGHindex-
6Throughput & ScalingMEDIUMthroughput-
7Global DistributionMEDIUMglobal-
8Monitoring & DiagnosticsLOW-MEDIUMmonitoring-
9Design PatternsHIGHpattern-
10Developer ToolingMEDIUMtooling-
11Vector SearchHIGHvector-
12Full-Text SearchHIGHfts-
13SecurityHIGHsecurity-

Quick Reference

1. Data Modeling (CRITICAL)

  • model-embed-related - Embed related data retrieved together
  • model-reference-large - Reference data when items get too large
  • model-avoid-2mb-limit - Keep items well under 2MB limit
  • model-id-constraints - Follow ID value length and character constraints
  • model-unique-keys - Enforce business-key uniqueness within the correct logical partition scope
  • model-nesting-depth - Stay within 128-level nesting depth limit
  • model-numeric-precision - Understand IEEE 754 numeric precision limits
  • model-denormalize-reads - Denormalize for read-heavy workloads including pre-computed aggregates
  • model-schema-versioning - Version your document schemas
  • model-type-discriminator - Use type discriminators for polymorphic data
  • model-json-serialization - Handle JSON serialization correctly for Cosmos DB documents
  • model-relationship-references - Use ID references with transient hydration for document relationships
  • model-ttl-expiration - Configure TTL relative to the item's last update, with container defaults and item overrides

2. Partition Key Design (CRITICAL)

  • partition-high-cardinality - Choose high-cardinality partition keys
  • partition-avoid-hotspots - Distribute writes evenly
  • partition-hierarchical - Use hierarchical partition keys for flexibility; order levels broad→narrow
  • partition-query-patterns - Align partition key with query patterns
  • partition-synthetic-keys - Create synthetic keys when needed
  • partition-key-length - Respect partition key value length limits
  • partition-immutable-key - Choose immutable properties as partition keys
  • partition-20gb-limit - Plan for 20GB logical partition limit
  • partition-rekey-migration - Re-key a misaligned container with the Change partition key feature

3. Query Optimization (HIGH)

  • query-aggregate-single-pass - Compute min/max/avg with one scoped aggregate query
  • query-avoid-cross-partition - Minimize cross-partition queries
  • query-use-projections - Project only needed fields; prefer dedicated result types for projections
  • query-pagination - Use continuation tokens for pagination
  • query-avoid-scans - Avoid full container scans
  • query-parameterize - Use parameterized queries
  • query-order-filters - Let the query engine order filters; tune indexed predicates rather than their textual order
  • query-top-literal - Use literal integers for TOP, never parameters
  • query-latest-by-timestamp - Query "latest" documents with explicit ORDER BY and TOP 1
  • query-olap-detection - Detect and redirect analytical queries away from transactional containers
  • query-point-reads - Use point reads (ReadItem) instead of queries when id and partition key are known
  • query-distinct-keyword - Return unique values with DISTINCT instead of client-side deduplication

4. SDK Best Practices (HIGH)

  • sdk-singleton-client - Reuse CosmosClient as singleton
  • sdk-async-api - Use async APIs for throughput
  • sdk-retry-429 - Handle 429s with retry-after
  • sdk-connection-mode - Use Direct mode for production
  • sdk-preferred-regions - Configure preferred regions
  • sdk-excluded-regions - Exclude regions experiencing issues
  • sdk-availability-strategy - Configure availability strategy for resilience
  • sdk-circuit-breaker - Use circuit breaker for fault tolerance
  • sdk-diagnostics - Log diagnostics for troubleshooting
  • sdk-serialization-enums - Serialize enums as strings not integers
  • sdk-emulator-ssl - Configure SSL and connection mode for Cosmos DB Emulator
  • sdk-ifnonematch-create - Use setIfNoneMatchETag("*") on createItem to reject duplicates atomically (409 on conflict)
  • sdk-no-shared-request-options - Use separate CosmosItemRequestOptions instances for deferred or concurrent createItem operations to avoid shared-state partition-key overrides
  • sdk-patch-incr - Use CosmosPatchOperations.increment() for atomic counter increments — no read RU, no ETag conflict cycle
  • sdk-bypage-empty-token - Guard against empty-string continuation tokens before calling byPage() — pass null for first page, never ""
  • sdk-etag-concurrency - Use ETags for optimistic concurrency on read-modify-write operations
  • sdk-java-content-response - Enable content response on write operations (Java)
  • sdk-java-cosmos-config - Configure Cosmos DB initialization correctly in Spring Boot
  • sdk-java-spring-boot-versions - Match Java version to Spring Boot requirements
  • sdk-local-dev-config - Configure local development to avoid cloud conflicts
  • sdk-dotnet-cosmos-package-id - Use Microsoft.Azure.Cosmos, not the abandoned Azure.Cosmos v4-preview package
  • sdk-newtonsoft-dependency - Explicitly reference Newtonsoft.Json package
  • sdk-python-async-deps - Include aiohttp when using Python async SDK
  • sdk-spring-data-annotations - Annotate entities for Spring Data Cosmos
  • sdk-spring-data-repository - Use CosmosRepository correctly and handle Iterable return types
  • sdk-langchain-cosmosdb-saver - Use CosmosDBSaver for LangGraph checkpointing with async container client
  • sdk-langchain-async-checkpointer - Initialize async Cosmos DB container in startup routine, not module level
  • sdk-langchain-mcp-persistent-session - Maintain persistent MCP client sessions for application lifetime
  • sdk-langchain-mcp-tool-content-format - Handle both string and list formats in MCP ToolMessage content
  • sdk-langgraph-mcp-tool-filtering - Filter MCP tools by name prefix for per-agent assignment
  • sdk-dotnet-namespace-collision - Avoid Microsoft.Azure.Cosmos namespace collisions with domain models (User, Database, Container, etc.)
  • sdk-ingestion-rate-control - Rate-control high-volume ingestion (concurrency, retry-after, throughput control)
  • sdk-go-partition-key-metadata - Upgrade stale Go SDKs to serialize partition-key kind metadata correctly across SDKs
  • sdk-python-client-kwargs - Configure Python v4 clients with flat keyword arguments instead of legacy ConnectionPolicy objects
  • sdk-langchain-js-chat-history - Persist isolated JS/TS conversations with AzureCosmosDBNoSQLChatMessageHistory
  • sdk-langchain-js-embedding-model - Configure the Azure OpenAI deployment name for JS/TS embeddings
  • sdk-langchain-js-filter-injection - Bind vector-store filter values through a parameterized filterClause
  • sdk-langchain-js-fulltext-prerequisites - Configure account capability, text policy, and indexes before hybrid search
  • sdk-langchain-js-managed-identity - Authenticate JS/TS LangChain with managed identity and data-plane RBAC
  • sdk-langchain-js-search-types - Choose vector, full-text, or hybrid retrieval for the query
  • sdk-langchain-js-semantic-cache - Reuse equivalent LLM responses with AzureCosmosDBNoSQLSemanticCache
  • sdk-langchain-js-vectorstore-init - Initialize JS/TS vector stores with embeddings and pre-provisioned RBAC resources

5. Indexing Strategies (MEDIUM-HIGH)

  • index-exclude-unused - Exclude paths never queried
  • index-path-syntax - Use correct path notation (/?, /[], /*)
  • index-composite - Use composite indexes for ORDER BY
  • index-composite-direction - Match composite index directions to ORDER BY
  • index-spatial - Add spatial indexes for geo queries
  • index-range-vs-hash - Choose appropriate index types
  • index-lazy-consistent - Understand indexing modes

6. Throughput & Scaling (MEDIUM)

  • throughput-autoscale - Use autoscale for variable workloads
  • throughput-right-size - Right-size provisioned throughput
  • throughput-serverless - Consider serverless for dev/test
  • throughput-burst - Understand burst capacity
  • throughput-container-vs-database - Choose allocation level wisely
  • throughput-idle-container-review - Review idle containers for lifecycle action
  • throughput-ttl-stale-data - Expire stale data with TTL before hitting storage limits
  • throughput-serverless-migration - Migrate a low-traffic provisioned account to serverless

7. Global Distribution (MEDIUM)

  • global-multi-region - Configure multi-region writes
  • global-consistency - Choose appropriate consistency level
  • global-conflict-resolution - Implement conflict resolution
  • global-failover - Configure automatic failover
  • global-read-regions - Add read regions near users
  • global-zone-redundancy - Enable zone redundancy for HA
  • global-multi-region-write-antipattern - Avoid multi-region writes without an active-active need

8. Monitoring & Diagnostics (LOW-MEDIUM)

  • monitoring-ru-consumption - Track RU consumption
  • monitoring-latency - Monitor P99 latency
  • monitoring-throttling - Alert on throttling
  • monitoring-azure-monitor - Integrate Azure Monitor
  • monitoring-diagnostic-logs - Enable diagnostic logging

9. Design Patterns (HIGH)

  • pattern-change-feed-materialized-views - Use Change Feed for cross-partition query optimization
  • pattern-efficient-ranking - Use count-based or cached approaches for efficient ranking
  • pattern-service-layer-relationships - Use a service layer to hydrate document references
  • pattern-langgraph-multi-agent - Use StateGraph with conditional edges for multi-agent routing
  • pattern-langgraph-interrupt-human - Use LangGraph interrupt for human-in-the-loop confirmation flows
  • pattern-langgraph-resume-checkpoint - Resume LangGraph from checkpoint after interrupt for multi-turn conversations
  • pattern-langgraph-agent-routing-cosmosdb - Persist active agent in Cosmos DB for deterministic routing via point reads
  • pattern-langgraph-fastapi-startup - Initialize LangGraph agents in FastAPI startup with retry logic
  • pattern-langgraph-chat-history-separate - Store chat history in a dedicated container, not the checkpointer
  • pattern-background-task-writes - Use FastAPI background tasks for non-blocking chat history writes
  • pattern-langgraph-async-cosmos-routing - Wrap Cosmos DB sync calls in asyncio.to_thread for LangGraph routing functions
  • pattern-langgraph-async-cosmos-writes - Use asyncio.to_thread for active agent writes in async node functions
  • pattern-langgraph-agent-name-attribution - Tag AI messages with agent name for API response attribution
  • pattern-ai-grounding-access - Use point reads for AI grounding when the item id and partition key are known

10. Developer Tooling (MEDIUM)

  • tooling-vscode-extension - Use the VS Code extension for routine inspection and management
  • tooling-emulator-setup - Use the Emulator for local development and testing
  • tooling-cosmosdb-shell-target - Verify the Cosmos DB Shell account and target before reading data
  • tooling-cosmosdb-shell-writes - Scope, preview, and confirm Cosmos DB Shell writes and deletions
  • tooling-cosmosdb-shell-setup - Diagnose Cosmos DB Shell MCP problems without changing the user's environment

11. Vector Search (HIGH)

  • vector-enable-feature - Enable vector search on the account before using vector features
  • vector-embedding-policy - Define vector embedding policy for vector properties
  • vector-index-type - Configure vector indexes in the indexing policy
  • vector-normalize-embeddings - Normalize embeddings for cosine similarity
  • vector-distance-query - Use VectorDistance for similarity search
  • vector-repository-pattern - Implement a repository pattern for vector search

12. Full-Text Search (HIGH)

  • fts-enable-capability - Enable EnableNoSQLFullTextSearch capability on the account — prerequisite for all FTS functions
  • fts-full-text-policy - Define fullTextPolicy on the container with correct language code (en-US, case-sensitive)
  • fts-index-policy - Add fullTextIndexes entry in the indexing policy to build the inverted index
  • fts-contains-query - Use FullTextContains / FullTextContainsAll / FullTextContainsAny instead of CONTAINS(LOWER(...))
  • fts-score-ranking - Use ORDER BY RANK FullTextScore(path, term) for BM25 relevance ranking
  • fts-hybrid-query - Combine FTS predicates with selective indexed equality/range filters; predicate text order does not control execution

13. Security (HIGH)

  • security-continuous-backup - Enable continuous backup for point-in-time recovery
  • security-disable-local-auth - Disable shared-key authentication after migrating clients to Entra ID
  • security-managed-identity - Use managed identity and DefaultAzureCredential instead of stored keys
  • security-network-restrict - Restrict public access and prefer private endpoints for production
  • security-private-endpoint-dns - Configure private DNS records and VNet links while retaining the account hostname
  • security-rbac-least-privilege - Assign narrowly scoped Cosmos DB data-plane roles

How to Use

Use the linked rule files above for detailed explanations and code examples. The links give the agent direct paths to the relevant guidance instead of relying on folder scanning or inferred filenames.

Each rule file contains:

  • Brief explanation of why it matters
  • Incorrect code example with explanation
  • Correct code example with explanation
  • Additional context and references

FAQ

What does the cosmosdb-best-practices skill do?

Azure Cosmos DB performance optimization and best practices guidelines for NoSQL, partitioning, queries, and SDK usage. Use when writing, reviewing, or refactoring code that interacts with Azure Cosmos DB, designing data models, optimizing queries, or implementing high-performance database operations. USE FOR: Cosmos DB NoSQL, partition key design, RU optimization, point reads, cross-partition queries, SDK singleton, CosmosClient, container modeling, change feed, bulk operations, vector search, full-text search, hierarchical partition keys, global distribution, autoscale throughput, indexing policy, DO NOT USE FOR: PostgreSQL, MySQL, MongoDB (non-Azure), DynamoDB, Cassandra, Azure SQL, Cosmos DB for PostgreSQL (vCore), Cosmos DB for MongoDB vCore, Azure DocumentDB, general SQL databases, Redis, Elasticsearch.

How do I install cosmosdb-best-practices?

Run npx skills add https://github.com/azurecosmosdb/cosmosdb-agent-kit --skill cosmosdb-best-practices in your project. The skills CLI detects the agents you use and copies the skill into the right folder. For Claude Code you can also place the skill folder at ~/.claude/skills/cosmosdb-best-practices/ (personal) or .claude/skills/cosmosdb-best-practices/ (project).

Which AI agents can use cosmosdb-best-practices?

Any agent that supports the open Agent Skills format (a folder with a SKILL.md file), including Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, Windsurf, Cline, OpenCode and more.

Is cosmosdb-best-practices safe to install?

No partner security audit is available yet. Skills can run commands and scripts, so read the SKILL.md and any bundled files before installing.

Who maintains cosmosdb-best-practices?

It is published by azurecosmosdb in azurecosmosdb/cosmosdb-agent-kit. It has 1,071 recorded installs.

More from azurecosmosdb/cosmosdb-agent-kit

View all →

cosmosdb-sdk

azurecosmosdb/cosmosdb-agent-kit

Open the skill to read its SKILL.md instructions.

47 installs

Popular code quality & workflow skills

View category →

grill-me

mattpocock/skills

A relentless interview to sharpen a plan or design.

Scan a codebase for deepening opportunities, present them as a visual HTML report, then grill through whichever one you pick.

handoff

mattpocock/skills

Compact the current conversation into a handoff document for another agent to pick up.

domain-modeling

mattpocock/skills

Build and sharpen a project's domain model. Use when discussing codebase terminology, writing or editing a GLOSSARY.md, or recording or editing an ADR.

codebase-design

mattpocock/skills

Shared vocabulary for designing deep modules. Use when the user wants to design or improve a module's interface, find deepening opportunities, decide where a seam goes, make code more testable or AI-navigable, or when another skill needs the deep-module vocabulary.

diagnosing-bugs

mattpocock/skills

Diagnosis loop for hard bugs and performance regressions. Use when the user says "diagnose"/"debug this", or reports something broken/throwing/failing/slow.