Python, from Zero, for AILesson 51 of 89

Module 6 · Talking to a service: HTTP from Python, done properly

Sessions: paying for the handshake once instead of every call

In this lesson

  • 7 min read
  • 6 reading sections
  • Knowledge check
  • Lesson 3 of 11 in this chapter

This chapter’s goalWrite a client for a paid model API that reuses connections, times out, retries only what is safe to retry, walks pagination with a generator, consumes a streamed response as it arrives, runs many requests concurrently under a rate limit, validates the JSON the model returns before trusting it, and reports the cost of every call from the usage figures in the response.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmWatch2:28Not available
Full audiobookNot availableNot available
One cold call from a laptop in Delhi to a server in VirginiaDNS lookup25TCP handshake, one roundtrip200TLS negotiation, two roundtrips400Your request and its reply220millisecondsOnly the last bar is your request. The first three happen before a byte of it moves, and on the secondcall through a Session they are all zero, because the connection is still open. That is why twohundred calls in a loop drop from roughly a minute and a half to well under one.
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.

Figure

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 8Free · no account needed
Lesson steps

What a single call does

requests.get(url) looks like one action. Underneath, it performs roughly this sequence:

  1. Resolve the hostname to an IP address (DNS).
  2. Open a TCP connection: one round trip to the server and back.
  3. Negotiate TLS, the encryption under https: one or two more round trips, plus some arithmetic on both ends.
  4. Send the request and wait for the response.
  5. Close the connection.

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.

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.

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Stuck on something?Ask Divya, your AI tutor

How to 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).