# npm/agent architecture

> Architecture diagram of the GitHub repository npm/agent, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/npm/agent
- Repository: https://github.com/npm/agent

## Overview

@npmcli/agent is a Node.js HTTP/HTTPS agent library. A client supplies an agent directly or asks getAgent to select, normalize, and reuse one; the agent then routes connections through direct sockets or an applicable proxy while providing DNS caching, keep-alives, and granular timeouts. The graph preserves proxy selection, DNS resolution, and error handling as distinct capabilities. Relationships follow the excerpts and README; there are no claims about unsampled integrations.

Main entry points: [`lib/agents.js`](https://github.com/npm/agent/blob/main/lib/agents.js) (HTTP agent), [`lib/index.js`](https://github.com/npm/agent/blob/main/lib/index.js) (Agent selection), [`lib/options.js`](https://github.com/npm/agent/blob/main/lib/options.js) (Option normalization), [`lib/proxy.js`](https://github.com/npm/agent/blob/main/lib/proxy.js) (Proxy selection)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Agent selection"]
  node_selection["Agent selection<br/>[index.js]"]
  node_options["Option normalization<br/>[options.js]"]
end

subgraph group_network["Connection handling"]
  node_agent["HTTP agent<br/>[agents.js]"]
  node_proxy["Proxy selection<br/>[proxy.js]"]
  node_proxyagents["Proxy agents<br/>[package.json]"]
  node_socket["TCP/TLS sockets<br/>[agents.js]"]
end

subgraph group_support["Network support"]
  node_dns[("DNS cache<br/>[dns.js]")]
  node_errors["Network errors<br/>[errors.js]"]
end

node_client(("HTTP client"))

node_client -->|"requests agent"| node_selection
node_client -->|"supplies directly"| node_agent
node_selection -->|"normalizes options"| node_options
node_selection -->|"selects proxy"| node_proxy
node_selection -->|"creates or reuses"| node_agent
node_selection -->|"exposes cache"| node_dns
node_agent -->|"normalizes options"| node_options
node_agent -->|"selects proxy"| node_proxy
node_agent -->|"connects through"| node_proxyagents
node_agent -->|"opens sockets"| node_socket
node_agent -->|"uses lookup"| node_dns
node_options -->|"gets lookup options"| node_dns
node_agent -->|"throws timeout errors"| node_errors
node_proxy -->|"throws protocol error"| node_errors

click node_selection "https://github.com/npm/agent/blob/main/lib/index.js"
click node_options "https://github.com/npm/agent/blob/main/lib/options.js"
click node_agent "https://github.com/npm/agent/blob/main/lib/agents.js"
click node_proxy "https://github.com/npm/agent/blob/main/lib/proxy.js"
click node_proxyagents "https://github.com/npm/agent/blob/main/package.json"
click node_socket "https://github.com/npm/agent/blob/main/lib/agents.js"
click node_dns "https://github.com/npm/agent/blob/main/lib/dns.js"
click node_errors "https://github.com/npm/agent/blob/main/lib/errors.js"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_selection,node_options,node_client toneBlue
class node_agent,node_proxy,node_proxyagents,node_socket toneAmber
class node_dns,node_errors toneMint
```

## Components

### Agent selection

- [Agent selection](https://github.com/npm/agent/blob/main/lib/index.js): `lib/index.js`
- [Option normalization](https://github.com/npm/agent/blob/main/lib/options.js): `lib/options.js`

### Connection handling

- [HTTP agent](https://github.com/npm/agent/blob/main/lib/agents.js): `lib/agents.js`
- [Proxy selection](https://github.com/npm/agent/blob/main/lib/proxy.js): `lib/proxy.js`
- [Proxy agents](https://github.com/npm/agent/blob/main/package.json): `package.json`
- [TCP/TLS sockets](https://github.com/npm/agent/blob/main/lib/agents.js): `lib/agents.js`

### Network support

- [DNS cache](https://github.com/npm/agent/blob/main/lib/dns.js): `lib/dns.js`
- [Network errors](https://github.com/npm/agent/blob/main/lib/errors.js): `lib/errors.js`

### Other components

- **HTTP client**

## Connections

- HTTP client → Agent selection: requests agent (evidence: `README.md`)
- HTTP client → HTTP agent: supplies directly (evidence: `README.md`)
- Agent selection → Option normalization: normalizes options (evidence: `lib/index.js`)
- Agent selection → Proxy selection: selects proxy (evidence: `lib/index.js`)
- Agent selection → HTTP agent: creates or reuses (evidence: `lib/index.js`)
- Agent selection → DNS cache: exposes cache (evidence: `lib/index.js`)
- HTTP agent → Option normalization: normalizes options (evidence: `lib/agents.js`)
- HTTP agent → Proxy selection: selects proxy (evidence: `lib/agents.js`)
- HTTP agent → Proxy agents: connects through (evidence: `lib/agents.js`)
- HTTP agent → TCP/TLS sockets: opens sockets (evidence: `lib/agents.js`)
- HTTP agent → DNS cache: uses lookup (evidence: `lib/agents.js`)
- Option normalization → DNS cache: gets lookup options (evidence: `lib/options.js`)
- HTTP agent → Network errors: throws timeout errors (evidence: `lib/agents.js`)
- Proxy selection → Network errors: throws protocol error (evidence: `lib/proxy.js`)
