Index of every page on this site: llms.txt
All skills
jeremylongshore avatar

/langchain-multi-env-setup

@0d57af8
Run it yourself

Build reliable dev / staging / prod isolation for LangChain 1.0 services — Pydantic Settings + SecretStr, cloud Secret Manager in prod, per-env prompt and model version pinning, env-specific checkpointer and observability. Use when graduating from .env-in-dev to real prod infra, or debugging a config that loaded the wrong values in the wrong env. Trigger with "langchain multi-env", "langchain pydantic settings", "langchain secret manager", "langchain env config", "langchain prod setup".

Use this Skill: https://skilld.dev/gh/jeremylongshore/tons-of-skills-marketplace/langchain-multi-env-setup

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencessecret-manager-integration.md

≈1.5k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Secret Manager Integration

Three backend patterns — GCP Secret Manager, AWS Secrets Manager, HashiCorp Vault. All three pull secrets into the Settings object only. None of them write to os.environ. That is the P37 fix.

Shared loader contract

def build_settings() -> Settings:
    env = os.environ.get("LANGCHAIN_ENV")
    if env == "prod":
        values = pull_from_secret_manager()   # backend-specific
        return Settings(**values)
    # dev / staging: see SKILL.md Step 2

The prod path is the only one that touches a secret backend. Settings(**values) populates the fields; values is thrown away when the function returns. Nothing writes os.environ[k] = v.

GCP Secret Manager

from google.cloud import secretmanager

def pull_from_secret_manager() -> dict[str, str]:
    client = secretmanager.SecretManagerServiceClient()
    project = os.environ["GCP_PROJECT_ID"]

    secret_names = [
        "ANTHROPIC_API_KEY",
        "OPENAI_API_KEY",
        "LANGSMITH_API_KEY",
    ]
    out: dict[str, str] = {}
    for name in secret_names:
        resource = f"projects/{project}/secrets/{name}/versions/latest"
        response = client.access_secret_version(request={"name": resource})
        out[name] = response.payload.data.decode("utf-8")

    # Non-secret passthrough (model id, prompt hash, endpoint URLs)
    for key in [
        "LANGCHAIN_ENV", "LANGCHAIN_MODEL_ID", "LANGCHAIN_PROMPT_COMMIT",
        "LANGCHAIN_VECTOR_INDEX", "LANGCHAIN_CHECKPOINTER_URL",
        "OTEL_EXPORTER_OTLP_ENDPOINT",
    ]:
        if key in os.environ:
            out[key] = os.environ[key]
    return out

Auth: Cloud Run and GKE workloads use the pod's service account automatically (workload identity). Grant roles/secretmanager.secretAccessor on each secret to the SA — not on the project. Audit trail lives in Cloud Audit Logs (secretmanager.secrets.access).

Rotation: Post a new version to the secret. The app re-reads at next restart because pull_from_secret_manager fetches versions/latest. For zero-downtime rotation, pin a specific version in LANGCHAIN_SECRET_VERSION and roll it via deploy — makes the rotation atomic with the deploy.

Cost: Secret Manager charges per access after the first 10k/month free. At one access per pod per start, this is effectively free.

AWS Secrets Manager

import boto3, json

def pull_from_secret_manager() -> dict[str, str]:
    client = boto3.client("secretsmanager", region_name=os.environ["AWS_REGION"])
    secret_id = os.environ["LANGCHAIN_SECRET_ID"]  # e.g. "prod/langchain/keys"
    response = client.get_secret_value(SecretId=secret_id)

    # Convention: one JSON blob containing all keys
    payload: dict[str, str] = json.loads(response["SecretString"])

    # Non-secret passthrough
    for key in [
        "LANGCHAIN_ENV", "LANGCHAIN_MODEL_ID", "LANGCHAIN_PROMPT_COMMIT",
        "LANGCHAIN_VECTOR_INDEX", "LANGCHAIN_CHECKPOINTER_URL",
        "OTEL_EXPORTER_OTLP_ENDPOINT",
    ]:
        if key in os.environ:
            payload[key] = os.environ[key]
    return payload

Auth: IAM role attached to the ECS task / EKS pod. Policy grants secretsmanager:GetSecretValue on the specific ARN.

Layout: One JSON blob per env (prod/langchain/keys, staging/langchain/keys) keeps the API calls low and the rotation story simple. Per-secret layout (prod/langchain/anthropic-key, prod/langchain/openai-key) is more IAM-granular but triples the startup latency. Most services prefer the blob.

Rotation: Use Secrets Manager's rotation Lambda for automatic rotation against Anthropic / OpenAI (they do not support it natively, so rotation is a human-in-the-loop action — the Lambda just schedules reminders).

HashiCorp Vault

import hvac

def pull_from_secret_manager() -> dict[str, str]:
    client = hvac.Client(url=os.environ["VAULT_ADDR"])
    # AppRole auth: role_id + secret_id from k8s-mounted files
    client.auth.approle.login(
        role_id=Path("/vault/role_id").read_text().strip(),
        secret_id=Path("/vault/secret_id").read_text().strip(),
    )

    response = client.secrets.kv.v2.read_secret_version(
        path=f"{os.environ['LANGCHAIN_ENV']}/langchain",
        mount_point="kv",
    )
    payload: dict[str, str] = response["data"]["data"]

    # Non-secret passthrough (same as GCP/AWS)
    for key in ["LANGCHAIN_ENV", "LANGCHAIN_MODEL_ID", ...]:
        if key in os.environ:
            payload[key] = os.environ[key]
    return payload

Auth: AppRole is the default for workloads. Kubernetes auth method works too — mount a service-account token and let Vault verify it via the k8s API.

Leases: The KV v2 secrets engine has no lease; values are static. For short-lived credentials (database users, cloud IAM), use dynamic secrets engines — out of scope for LangChain key material.

What NOT to do

Anti-pattern Why it is broken
os.environ[k] = v after pulling from Secret Manager Re-introduces P37 — docker exec env can read it back
Writing the secret to a tmpfs file and reading it on each request Extra IO per request; file still visible to sidecars; no value over SecretStr
Storing the plaintext in a class attribute (Settings.anthropic_api_key: str) Loses SecretStr masking — repr(settings) leaks it
Hard-coding the secret resource path Makes rotation a code change instead of a config change
Pulling on every request Rate limits the Secret Manager backend; adds 50-200ms to every call; pull once at startup
Caching the dict globally with the plaintext values Defeats SecretStr — keep plaintext only inside the Settings field

Observability note

The Secret Manager access is worth logging at INFO level with the resource name (not the value). One line at startup is enough:

logger.info("pulled %d secrets from %s", len(out), "gcp_secret_manager")

If the log line never appears, something is wrong with the loader branch — the service started in dev or staging mode when ops thought it was prod. Cheap signal, catches a real class of misconfiguration.

Source: SKILL.md on GitHub

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts2mo3 checks · Risk SAFE
  • Gen Agent Trust Hub2mo

    This skill implements security best practices for configuration management in LangChain applications, focusing on preventing credential leakage and ensuring environment isolation using Pydantic and cloud secret managers.

  • Socket2mo

    No alerts

  • Snyk2mo

    Risk: LOW · No issues

Signed by skilld at 0d57af8. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub yesterday.

Activeupdated 2 months ago
What it can do
Reads files Edits files Runs commands
version
2.7.0
author
Jeremy Longshore <jeremy@intentsolutions.io>
compatibility
Designed for Claude Code
All 5 allowed tools
ReadWriteEditBash(python:*)Bash(gcloud:*)
Other metadata
tags
[
  "saas",
  "langchain",
  "langgraph",
  "python",
  "langchain-1.0",
  "config",
  "pydantic",
  "multi-env",
  "secrets"
]

README badge

README badge for jeremylongshore/tons-of-skills-marketplace/langchain-multi-env-setup