{"course":{"slug":"python-for-ai","title":"Python, from Zero, for AI","url":"https://addaly.com/learn/python-for-ai","level":"beginner","track":"build"},"lesson":{"slug":"sessions-and-connection-reuse","title":"Sessions: paying for the handshake once instead of every call","url":"https://addaly.com/learn/python-for-ai/sessions-and-connection-reuse","position":51,"of":89,"minutes":7,"answer":"A bare requests.get() opens and closes a TCP and TLS connection every time; a Session keeps it open.","keyIdea":"A bare requests.get() opens and closes a TCP and TLS connection every time; a Session keeps it open, which is why a loop of two hundred calls drops from minutes to seconds and why every serious client is a Session."},"body":{"format":"markdown","text":"## What a single call does\n\n`requests.get(url)` looks like one action. Underneath, it performs roughly this sequence:\n\n1. Resolve the hostname to an IP address (DNS).\n2. Open a TCP connection: one round trip to the server and back.\n3. Negotiate TLS, the encryption under `https`: one or two more round trips, plus some arithmetic on both ends.\n4. Send the request and wait for the response.\n5. Close the connection.\n\nSteps 1 to 3 happen before a single byte of your request moves. From a laptop in Delhi to a server in Virginia, a round trip is around 200 ms, so a bare call to a fast API can spend 400 to 600 ms setting up and 40 ms doing the work. Then step 5 throws the connection away, and the next call starts from nothing.\n\nFor one call, nobody cares. For a loop of five hundred, this is the difference between a script that finishes over coffee and one that finishes tomorrow.\n\n> Figure: One cold call from a laptop in Delhi to a server in Virginia. Bars (milliseconds): DNS lookup 25; TCP handshake, one round trip 200; TLS negotiation, two round trips 400; Your request and its reply 220. Only the last bar is your request. The first three happen before a byte of it moves, and on the second call through a Session they are all zero, because the connection is still open. That is why two hundred calls in a loop drop from roughly a minute and a half to well under one.\n\n## The Session\n\n```python\nimport requests\n\nsession = requests.Session()\nsession.headers.update({\n    \"Authorization\": f\"Bearer {key}\",\n    \"User-Agent\": \"my-tagger/0.1\",\n})\n\nfor item in items:\n    r = session.post(url, json={\"text\": item}, timeout=30)\n    r.raise_for_status()\n    results.append(r.json())\n```\n\nThree things changed.\n\nThe connection stays open between calls. HTTP calls this **keep-alive**, and every modern server supports it. The second call to the same host skips DNS, TCP and TLS entirely.\n\nThe headers are set once on the session and sent with every request, so a forgotten header on one call is no longer possible.\n\nThe Session also holds cookies, which matters for a few APIs that set one on login and expect it back.\n\nMeasure it yourself:\n\n```python\nimport time, requests\n\nurl = \"https://httpbin.org/get\"\n\nt = time.perf_counter()\nfor _ in range(20):\n    requests.get(url, timeout=10)\nprint(\"bare:   \", round(time.perf_counter() - t, 2), \"s\")\n\nt = time.perf_counter()\nwith requests.Session() as s:\n    for _ in range(20):\n        s.get(url, timeout=10)\nprint(\"session:\", round(time.perf_counter() - t, 2), \"s\")\n```\n\nThe gap depends on how far the server is. It is rarely less than two to one.\n\n## The pool underneath\n\nA Session does not hold one connection; it holds a small **pool** of them per host, ten by default. If you later run requests on several threads, the pool hands each thread a connection and opens more when they run out, up to a limit. When more threads want a connection than the pool has, the extras wait, and `requests` logs a warning about the pool being full. Raising the limit is a one-line change:\n\n```python\nfrom requests.adapters import HTTPAdapter\n\nsession.mount(\"https://\", HTTPAdapter(pool_connections=20, pool_maxsize=20))\n```\n\nThis is also where retries are configured, which the next lesson does.\n\n## Close it\n\nA Session holds open sockets. On a short script the operating system reclaims them when the process exits, and nobody notices. In a long-running program that creates a new Session per request and never closes it, sockets accumulate until the process runs out of file descriptors and every network call starts failing with a message about \"too many open files\". The fix is to create one Session for the life of the program, or to use it as a context manager so it closes itself:\n\n```python\nwith requests.Session() as session:\n    ...\n```\n\nCreate it once at module level or pass it in; do not create one inside the function that makes the call.\n\n## What the provider SDKs do\n\nIf you use a provider's own library (`openai`, `anthropic`, `httpx` under both) this is already handled. The client object you construct once is the session, with the pool, keep-alive and default headers. Which is one reason to construct it once and reuse it, rather than building a new client inside every function. A later lesson covers what else those clients do for you.\n\n## Where the handshake still happens\n\nKeep-alive is a request, not a guarantee. Servers close idle connections after a while, often 60 seconds, and some proxies and load balancers close them sooner. A Session that sits idle for five minutes will pay the handshake again on its next call, and `requests` handles that silently. Occasionally the server closes the connection at the exact moment you send on it, and you see a `ConnectionError` with \"Connection reset by peer\" or \"RemoteDisconnected\". That is not your bug. It is the reason retrying an idempotent request once is reasonable, which is the next lesson's subject.\n\n## Try this now\n\nTake the loop from your first API call and move it onto a Session. Time both versions with `time.perf_counter()`. Then set a header on the session, make a call, and print `r.request.headers` to confirm the header went out without you passing it."},"check":{"question":"A script makes 300 small GET calls to the same API in a loop, each taking about 250 ms, of which the server's own work is about 40 ms. Moving the loop onto a `requests.Session()` brings each call to about 60 ms. What explains most of the saving?","options":[{"text":"The Session caches response bodies, so repeated calls to the same host are answered from memory without contacting the server at all, which removes the network wait entirely.","correct":false,"tempting":"A Session has no response cache; every call still goes to the server and receives a fresh reply."},{"text":"Each bare call paid a fresh TCP and TLS handshake before sending anything; the Session keeps the connection open and reuses it.","correct":true},{"text":"The Session compresses the request bodies, and the server decompresses them faster than it parses plain text.","correct":false,"tempting":"Request compression is not something requests does by default, and these are GET calls with no body to compress."},{"text":"The Session runs the calls on several threads at once, so the loop overlaps the waiting.","correct":false,"tempting":"A Session is synchronous; the loop still waits for each call to finish before starting the next."}]},"prev":{"slug":"anatomy-of-a-request","title":"What a request is made of, and how to see the one you actually sent","url":"https://addaly.com/learn/python-for-ai/anatomy-of-a-request"},"next":{"slug":"timeouts-retries-and-backoff","title":"Timeouts and retries: the code that decides whether you pay twice","url":"https://addaly.com/learn/python-for-ai/timeouts-retries-and-backoff"},"authorship":"Founder and series editor: Dr Mustafa Mun. Written by AI models to a written standard. Source and assessment review continues. Academic review invited.","review":{"status":"Draft, open for academic review"},"cite":"Addaly (2026) 'Sessions: paying for the handshake once instead of every call', in Python, from Zero, for AI. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/python-for-ai/sessions-and-connection-reuse (Accessed: 21 November 2026).","licence":{"name":"CC BY-NC-SA 4.0","url":"https://creativecommons.org/licenses/by-nc-sa/4.0/"},"reader":{"schema":1,"title":"Sessions: paying for the handshake once instead of every call","courseTitle":"Python, from Zero, for AI","module":{"index":6,"slug":"talking-to-a-service","title":"Talking to a service: HTTP from Python, done properly"},"answer":null,"keyIdea":"A bare requests.get() opens and closes a TCP and TLS connection every time; a Session keeps it open, which is why a loop of two hundred calls drops from minutes to seconds and why every serious client is a Session.","hero":{"kind":"figure","figure":{"t":"figure","label":"Figure","figure":{"caption":"Only the last bar is your request. The first three happen before a byte of it moves, and on the second call through a Session they are all zero, because the connection is still open. That is why two hundred calls in a loop drop from roughly a minute and a half to well under one.","series":[{"label":"DNS lookup","value":25},{"label":"TCP handshake, one round trip","value":200},{"label":"TLS negotiation, two round trips","value":400},{"highlight":true,"label":"Your request and its reply","value":220}],"title":"One cold call from a laptop in Delhi to a server in Virginia","type":"bars","unit":"milliseconds"},"alt":"Figure: One cold call from a laptop in Delhi to a server in Virginia. Bars (milliseconds): DNS lookup 25; TCP handshake, one round trip 200; TLS negotiation, two round trips 400; Your request and its reply 220. Only the last bar is your request. The first three happen before a byte of it moves, and on the second call through a Session they are all zero, because the connection is still open. That is why two hundred calls in a loop drop from roughly a minute and a half to well under one."}},"blocks":[{"t":"h2","id":"what-a-single-call-does","runs":[{"text":"What a single call does"}]},{"t":"p","runs":[{"text":"requests.get(url)","code":true},{"text":" looks like one action. Underneath, it performs roughly this sequence:"}]},{"t":"ol","start":1,"items":[[{"text":"Resolve the hostname to an IP address (DNS)."}],[{"text":"Open a TCP connection: one round trip to the server and back."}],[{"text":"Negotiate TLS, the encryption under "},{"text":"https","code":true},{"text":": one or two more round trips, plus some arithmetic on both ends."}],[{"text":"Send the request and wait for the response."}],[{"text":"Close the connection."}]]},{"t":"p","runs":[{"text":"Steps 1 to 3 happen before a single byte of your request moves. From a laptop in Delhi to a server in Virginia, a round trip is around 200 ms, so a bare call to a fast API can spend 400 to 600 ms setting up and 40 ms doing the work. Then step 5 throws the connection away, and the next call starts from nothing."}]},{"t":"p","runs":[{"text":"For one call, nobody cares. For a loop of five hundred, this is the difference between a script that finishes over coffee and one that finishes tomorrow."}]},{"t":"h2","id":"the-session","runs":[{"text":"The Session"}]},{"t":"code","lang":"python","label":"Python","text":"import requests\n\nsession = requests.Session()\nsession.headers.update({\n    \"Authorization\": f\"Bearer {key}\",\n    \"User-Agent\": \"my-tagger/0.1\",\n})\n\nfor item in items:\n    r = session.post(url, json={\"text\": item}, timeout=30)\n    r.raise_for_status()\n    results.append(r.json())"},{"t":"p","runs":[{"text":"Three things changed."}]},{"t":"p","runs":[{"text":"The connection stays open between calls. HTTP calls this "},{"text":"keep-alive","b":true},{"text":", and every modern server supports it. The second call to the same host skips DNS, TCP and TLS entirely."}]},{"t":"p","runs":[{"text":"The headers are set once on the session and sent with every request, so a forgotten header on one call is no longer possible."}]},{"t":"p","runs":[{"text":"The Session also holds cookies, which matters for a few APIs that set one on login and expect it back."}]},{"t":"p","runs":[{"text":"Measure it yourself:"}]},{"t":"code","lang":"python","label":"Python","text":"import time, requests\n\nurl = \"https://httpbin.org/get\"\n\nt = time.perf_counter()\nfor _ in range(20):\n    requests.get(url, timeout=10)\nprint(\"bare:   \", round(time.perf_counter() - t, 2), \"s\")\n\nt = time.perf_counter()\nwith requests.Session() as s:\n    for _ in range(20):\n        s.get(url, timeout=10)\nprint(\"session:\", round(time.perf_counter() - t, 2), \"s\")"},{"t":"p","runs":[{"text":"The gap depends on how far the server is. It is rarely less than two to one."}]},{"t":"h2","id":"the-pool-underneath","runs":[{"text":"The pool underneath"}]},{"t":"p","runs":[{"text":"A Session does not hold one connection; it holds a small "},{"text":"pool","b":true},{"text":" of them per host, ten by default. If you later run requests on several threads, the pool hands each thread a connection and opens more when they run out, up to a limit. When more threads want a connection than the pool has, the extras wait, and "},{"text":"requests","code":true},{"text":" logs a warning about the pool being full. Raising the limit is a one-line change:"}]},{"t":"code","lang":"python","label":"Python","text":"from requests.adapters import HTTPAdapter\n\nsession.mount(\"https://\", HTTPAdapter(pool_connections=20, pool_maxsize=20))"},{"t":"p","runs":[{"text":"This is also where retries are configured, which the next lesson does."}]},{"t":"h2","id":"close-it","runs":[{"text":"Close it"}]},{"t":"p","runs":[{"text":"A Session holds open sockets. On a short script the operating system reclaims them when the process exits, and nobody notices. In a long-running program that creates a new Session per request and never closes it, sockets accumulate until the process runs out of file descriptors and every network call starts failing with a message about “too many open files”. The fix is to create one Session for the life of the program, or to use it as a context manager so it closes itself:"}]},{"t":"code","lang":"python","label":"Python","text":"with requests.Session() as session:\n    ..."},{"t":"p","runs":[{"text":"Create it once at module level or pass it in; do not create one inside the function that makes the call."}]},{"t":"h2","id":"what-the-provider-sdks-do","runs":[{"text":"What the provider SDKs do"}]},{"t":"p","runs":[{"text":"If you use a provider’s own library ("},{"text":"openai","code":true},{"text":", "},{"text":"anthropic","code":true},{"text":", "},{"text":"httpx","code":true},{"text":" under both) this is already handled. The client object you construct once is the session, with the pool, keep-alive and default headers. Which is one reason to construct it once and reuse it, rather than building a new client inside every function. A later lesson covers what else those clients do for you."}]},{"t":"h2","id":"where-the-handshake-still-happens","runs":[{"text":"Where the handshake still happens"}]},{"t":"p","runs":[{"text":"Keep-alive is a request, not a guarantee. Servers close idle connections after a while, often 60 seconds, and some proxies and load balancers close them sooner. A Session that sits idle for five minutes will pay the handshake again on its next call, and "},{"text":"requests","code":true},{"text":" handles that silently. Occasionally the server closes the connection at the exact moment you send on it, and you see a "},{"text":"ConnectionError","code":true},{"text":" with “Connection reset by peer” or “RemoteDisconnected”. That is not your bug. It is the reason retrying an idempotent request once is reasonable, which is the next lesson’s subject."}]},{"t":"h2","id":"try-this-now","runs":[{"text":"Try this now"}],"role":"try"},{"t":"p","runs":[{"text":"Take the loop from your first API call and move it onto a Session. Time both versions with "},{"text":"time.perf_counter()","code":true},{"text":". Then set a header on the session, make a call, and print "},{"text":"r.request.headers","code":true},{"text":" to confirm the header went out without you passing it."}]}],"check":{"question":[{"text":"A script makes 300 small GET calls to the same API in a loop, each taking about 250 ms, of which the server’s own work is about 40 ms. Moving the loop onto a "},{"text":"requests.Session()","code":true},{"text":" brings each call to about 60 ms. What explains most of the saving?"}],"options":[{"text":[{"text":"The Session caches response bodies, so repeated calls to the same host are answered from memory without contacting the server at all, which removes the network wait entirely."}],"correct":false,"tempting":[{"text":"A Session has no response cache; every call still goes to the server and receives a fresh reply."}]},{"text":[{"text":"Each bare call paid a fresh TCP and TLS handshake before sending anything; the Session keeps the connection open and reuses it."}],"correct":true,"tempting":null},{"text":[{"text":"The Session compresses the request bodies, and the server decompresses them faster than it parses plain text."}],"correct":false,"tempting":[{"text":"Request compression is not something requests does by default, and these are GET calls with no body to compress."}]},{"text":[{"text":"The Session runs the calls on several threads at once, so the loop overlaps the waiting."}],"correct":false,"tempting":[{"text":"A Session is synchronous; the loop still waits for each call to finish before starting the next."}]}],"order":[0,1,3,2],"why":null},"prev":{"slug":"anatomy-of-a-request","title":"What a request is made of, and how to see the one you actually sent","minutes":8},"next":{"slug":"timeouts-retries-and-backoff","title":"Timeouts and retries: the code that decides whether you pay twice","minutes":9}}}