In this paper, the real-time energy trading problem between the energy provider and the consumers in a smart grid system is studied. The problem is formulated as a hierarchical game, where the energy provider acts as a leader who determines the pricing strategy that maximizes its profits, while the consumers act as followers who react by adjusting their energy demand to save their energy costs and enhance their energy consumption utility. In particular, the energy provider employs a pricing strategy that depends on the aggregated amount of energy requested by the consumers, which suits a commodity-limited market. With this price setting, the consumers’ energy demand response strategies are designed under a non-cooperative game framework, where a unique generalized Nash equilibrium point is shown to exist. As an extension, the consumers are assumed to be unaware of their future energy consumption behaviors due to uncertain personal needs. To address this issue, an online distributed energy trading framework is proposed, where the energy provider and the consumers can design their strategies only based on the historical knowledge of consumers’ energy consumption behavior at each bidding stage. Besides, the proposed framework can be implemented in a distributed manner such that the consumers can design their demand responses by only exchanging information with their neighboring consumers, which requires much fewer communication resources and would thus be more suitable for the practical operation of the grid. As a theoretical guarantee, the proposed framework is further proved to asymptotically achieve the same performance as the offline solution for both energy provider and consumers’ optimization problems. The performance of practical designs of the proposed online distributed energy trading framework is finally illustrated in numerical experiments.